Solved

XmlWriter - Create Empty Tag <tag></tag>

Posted on 2012-03-20
4
3,545 Views
Last Modified: 2012-06-27
Hello everybody, im trying to serialize a class and everything works as expected, however, the empty string properties are being serialized like this:

<Element />

and i need it to be like this:

<Element></Element>

I saw that using the XmlTextWriter i could do such a thing, but since i am also using the XmlWriterSettings, im not sure how to do it.. here is how i am doing it right now:

XmlSerializer s = new XmlSerializer(typeof(MY_TYPE));
using (FileStream fs = new FileStream(File, FileMode.CreateNew))
{
          XmlWriterSettings settings = new XmlWriterSettings();
          settings.Encoding = Encoding.GetEncoding("ISO-8859-1");
          settings.NewLineChars = Environment.NewLine;
          settings.ConformanceLevel = ConformanceLevel.Document;
          settings.Indent = true;
          using (XmlWriter writer = XmlWriter.Create(fs, settings))
          {
                        s.Serialize(writer, this);
           }
}

Open in new window

0
Comment
Question by:eguilherme
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 21

Expert Comment

by:Alfred1
ID: 37745491
Since you are using XmlWriter, you can use XmlWriter.WriteFullEndElement Method

Check the link below:

http://msdn.microsoft.com/en-us/library/system.xml.xmlwriter.writefullendelement.aspx
0
 
LVL 21

Expert Comment

by:Alfred1
ID: 37745513
Just to add in my previous comment, I didn't realise that you are trying to force XMLSerialization to produce the explicit closing tag.   Check the link below:

http://bytes.com/topic/net/answers/178893-force-xmlserializer-use-explicit-closing-tags-zero-length-strings

I hope this helps.
0
 
LVL 10

Author Comment

by:eguilherme
ID: 37746771
@Alfred1
I saw that link too.. but using the new XmlTextWritter like in there, i cant set the XmlWritterSettings like im doing today to configure the way the xml is being generated...
0
 
LVL 21

Accepted Solution

by:
Alfred1 earned 500 total points
ID: 37748057
OK.  I am providing a wrapper code for the original XmlWriter called XmlWriterEE that would force WriteEndElement to use WriteFullEndElement.   Check it out.

            [embed=snippet 8250311]

Open in new window

XmlSerializer s = new XmlSerializer(typeof(MY_TYPE));
            using (FileStream fs = new FileStream(File, FileMode.CreateNew))
            {
                XmlWriterSettings settings = new XmlWriterSettings();
                settings.Encoding = Encoding.GetEncoding("ISO-8859-1");
                settings.NewLineChars = Environment.NewLine;
                settings.ConformanceLevel = ConformanceLevel.Document;
                settings.Indent = true;
                using (XmlWriter writer = XmlWriter.Create(fs, settings))
                {
                    s.Serialize(new XmlWriterEE(writer),this);
                }
            }

    //---------------------------------------------------------------------------------

    public class XmlWriterEE :  XmlWriter
    {
        private XmlWriter baseWriter;

        public XmlWriterEE(XmlWriter w)
        {
            baseWriter = w;
        }

        //Force WriteEndElement to use WriteFullEndElement
        public override void WriteEndElement() {baseWriter.WriteFullEndElement(); }

        public override void WriteFullEndElement()
        {
            baseWriter.WriteFullEndElement();
        }

        public override void Close()
        {
            baseWriter.Close();
        }

        public override void Flush()
        {
            baseWriter.Flush();
        }

        public override string LookupPrefix(string ns)
        {
            return (baseWriter.LookupPrefix(ns));
        }

        public override void WriteBase64(byte[] buffer, int index, int count)
        {
            baseWriter.WriteBase64(buffer, index, count);
        }

        public override void WriteCData(string text)
        {
            baseWriter.WriteCData(text);
        }

        public override void WriteCharEntity(char ch)
        {
            baseWriter.WriteCharEntity(ch);
        }

        public override void WriteChars(char[] buffer, int index, int count)
        {
            baseWriter.WriteChars(buffer, index, count);
        }

        public override void WriteComment(string text)
        {
            baseWriter.WriteComment(text);
        }

        public override void WriteDocType(string name, string pubid, string sysid, string subset)
        {
            baseWriter.WriteDocType(name, pubid, sysid, subset);
        }

        public override void WriteEndAttribute()
        {
            baseWriter.WriteEndAttribute();
        }

        public override void WriteEndDocument()
        {
            baseWriter.WriteEndDocument();
        }

        public override void WriteEntityRef(string name)
        {
            baseWriter.WriteEntityRef(name);
        }

        public override void WriteProcessingInstruction(string name, string text)
        {
            baseWriter.WriteProcessingInstruction(name, text);
        }

        public override void WriteRaw(string data)
        {
            baseWriter.WriteRaw(data);
        }

        public override void WriteRaw(char[] buffer, int index, int count)
        {
            baseWriter.WriteRaw(buffer, index, count);
        }

        public override void WriteStartAttribute(string prefix, string localName, string ns)
        {
            baseWriter.WriteStartAttribute(prefix, localName, ns);
        }

        public override void WriteStartDocument(bool standalone)
        {
            baseWriter.WriteStartDocument(standalone);
        }

        public override void WriteStartDocument()
        {
            baseWriter.WriteStartDocument();
        }

        public override void WriteStartElement(string prefix, string localName, string ns)
        {
            baseWriter.WriteStartElement(prefix, localName, ns);
        }

        public override WriteState WriteState
        {
            get { return baseWriter.WriteState; }
        }

        public override void WriteString(string text)
        {
            baseWriter.WriteString(text);
        }

        public override void WriteSurrogateCharEntity(char lowChar, char highChar)
        {
            baseWriter.WriteSurrogateCharEntity(lowChar, highChar);
        }

        public override void WriteWhitespace(string ws)
        {
            baseWriter.WriteWhitespace(ws);
        }
    }

Open in new window

0

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question