Solved

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

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

729 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