Solved

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

Posted on 2012-03-20
4
3,303 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
  • 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

708 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now