How make XMLDocument do not put spaces on self-closed tags?

此生再无相见时 提交于 2019-12-22 05:08:14

问题


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(" />","/>");



来源:https://stackoverflow.com/questions/4881564/how-make-xmldocument-do-not-put-spaces-on-self-closed-tags

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!