I have a strange problem with my customer - I am reading an XML document (actually an InfoPath document) with XmlSerializer
, modifying it, then writing out an XML document using the XmlSerializer
, and then add some processing instructions by using the XmlTextWriter
. All works well, and the resulting document is actually fully XML conform and can be read by InfoPath. One change that happens in the structure is however that the original document has all empty tags written in the form <A></A>
, and when my document is written, it becomes <A/>
. Actually exactly the same due to XML standards. But, my customer (a big company) apparently has some check/validation scripts with hardcoded <A></A>
, and they fail. He's is now upset, and is too lazy to change his scripts, and wants the <A></A>
notation! How can I setup XmlTextWriter
to do it?
You can do this by providing XmlSerializer
with your own XmlWriter
descendant, which would override the WriteEndElement
method. By default, this method produces an empty element (<a />
) or a closing element (</a>
) depending on the contents of the element being output. You can change the behavior to always produce a full closing element.
public class MyXmlWriter : XmlTextWriter
{
// …constructors etc. omitted for the sake of simplicity…
public override void WriteEndElement()
{
// this should do the trick
WriteFullEndElement();
}
}
However, there's some more work needed, e.g. the Async
version of this method, but in principle, it should be easy to achieve like above. I'd recommend looking into the source code of XmlTextWriter.WriteEndElement
and deriving a version that better fits your case.