I have a situation where XML data is being processed by two different mechanisms. In one place it is being processed using Python's xml.dom.minidom library. In the other, similar processing is being performed in .NET, via an XmlTextWriter.
In the output generated by the Python code, empty elements are written <ElementName/> (with no space before the element close). In the .NET code, a space is being inserted (resulting in <ElementName />). This makes no difference whatsoever to the validity or meaning of the XML, but it does cause the output to be detected as different when the two outputs are compared.
Is there any way to tell the XmlTextWriter not to include the extra space? Failing that, is there any way to include the extra space in the Python-generated output (short of messing with the library source, which wile possible is something I consider undesirable ;-))?
Update: Perhaps I should explain what I'm trying to do instead of just describing the problem. It's possible that I'm making things more complicated / painful than I should.
What I really need is some mechanism to determine that the structure represented by the XML has not been modified. I was originally flattening the XML (which eliminated whitespace issues when everything was being done in .NET world), then calculating an appropriately salted hash of the data. Is there a better mechanism I could / should be using?
You'll find that the problem only occurs if you set the Indent property in the XmlWriterSettings to true. When Indent == false, there is no space inserted. But if you want indentation, you have to live with that space.
So perhaps the solution to your program is to turn off indentation in both tools?
This is unfortunate, because it's almost possible to change that behavior.
The implementation of XmlWriter actually calls XmlWriterSettings.CreateWriter to create a writer based on the settings you pass. If Indent == true, then it creates an XmlEncodedRawTextWriterIndent, which is an internal class derived from the abstract XmlWriter. It overrides WriteFullEndElement and inserts that space.
In theory, you could create your own class derived from XmlEncodedRawTextWriterIndent that overrides WriteFullEndElement. If you could do that, it'd be easy to prevent the indentation. But you can't do that because it's an internal class (internal to System.Xml). Even if you could subclass XmlEncodedRawTextWriterIndent, you'd have the problem that XmlWriterSettings.CreateXmlWriter doesn't have a way to instantiate your class, and XmlWriterSettings is sealed.
I imagine there are good reasons for effectively preventing creation of custom XmlWriter classes, although they escape me at the moment.
Can I tell an XmlTextWriter to write  instead of ?