Derek Harmon
<scott.ballard@gmail.com> wrote in message news:1110988470.845630.79610@z14g2000cwz.googlegro ups.com...[color=blue]
> The problem is that when a string contains a zero-length string ("") it is serialized as:
> <foo />
> When what I would really like is:
> <foo></foo>[/color]
Scott,
You can control this by subclassing XmlTextWriter to inter-
cept all calls to WriteFullEndElement( ) and substitute a call
to WriteEndElement( ) instead. WriteEndElement( ) is smart
enough to know whether it must write a full end tag or not.
- - - XmlTextWriterEE.cs (complete)
using System;
using System.IO;
using System.Xml;
namespace Derek.Xml
{
/// <summary>
/// Wrapper that forces more compact empty element end
/// tags to be written whenever possible.
/// </summary>
public class XmlTextWriterEE : XmlTextWriter
{
public XmlTextWriterEE( TextWriter sink) : base( sink) {*;}
public override void WriteFullEndElement( ) {
base.WriteEndElement( );
}
}
}
- - -
In your case you would use this class in the project and wrap
the TextWriter you may already using in an XmlTextWriterEE
when calling Serialize( ),
serializer.Serialize( new XmlTextWriterEE( destTextWriter), obj);
Derek Harmon |