Solved

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

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SharePoint 2013 Blog Template 2 37
Remove greater than sign 3 49
Error in script 11 48
JQuery to parse xml string and get element by id 4 24
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
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…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

863 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

24 Experts available now in Live!

Get 1:1 Help Now