Link to home
Start Free TrialLog in
Avatar of Edgard Yamashita
Edgard YamashitaFlag for Brazil

asked on

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

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

Avatar of Alfred A.
Alfred A.
Flag of Australia image

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
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.
Avatar of Edgard Yamashita

ASKER

@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...
ASKER CERTIFIED SOLUTION
Avatar of Alfred A.
Alfred A.
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial