How make XMLDocument do not put spaces on self-clo

2019-04-28 22:21发布

问题:

I have an XML well formatted without any spaces. It' must be like that.

When I load it to XMLDocument to sign, the self-closing tags gets an extra white space and

    <cEAN/>

becomes:

     <cEAN />

Once this document must be signed, it's impossible to remove the white space.

The property PreserveWhiteSpace doesn't made any difference to the result.

How can I change this behavior?

回答1:

There is no space before the closing "/" in the XmlDocument. XmlDocument is a data structure consisting of nodes. It is binary. It is not text.

Any extra space you are seeing exists only when you serialize the document as text.

Are you actually having a problem with signing, or do you only think you will have such a problem?



回答2:

I have had this problem before. XML signed by a basic Hash so it can't change when serialized. I solved it by writing a serializer so that I could be sure that it would output the correct XML.

The basic recipe is to Read the XML with a XMLReader, and write out each chunk as it comes.



回答3:

Try this:

XMLDocument doc;

...

string XMLstring = doc.OuterXml.Replace(" />","/>");