0

How can I avoid XmlDocument class replace ' entity with the ' character? For example if I have:

string xml = "<a> &apos; </a>";

After doing

var doc = new XmlDocument();
doc.LoadXml(xml);
string output = doc.OutterXml;

The value of output is

"<a>'</a>"

I need to avoid this because I must load an XML, make some changes and sign it digitally so the signed XML must be the same loaded.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
hdkrus
  • 420
  • 1
  • 5
  • 13
  • 4
    You can’t make changes to an XML document and sign it so that it’s the same as it was before the changes. That’s the definition of a change. So… could you explain that part in more detail, please? `'` and `'` should be interchangeable in element text. – Ry- Sep 02 '14 at 22:18
  • Also, is there a reason you're using `XmlDocument` instead of `XDocument`? – John Saunders Sep 02 '14 at 22:21
  • Indeed the sign will be applied to XML after my addings so that wont be problem, the problem is if XML comes originally with ' and after loading in XmlDocument object it changes to ' – hdkrus Sep 02 '14 at 22:24
  • Yes there are some reasons for using XmlDocument instead of XDocument but anyway I tested with XDocument and the result is the same. – hdkrus Sep 02 '14 at 22:25
  • 1
    I wonder why people does not provide an answer and just downvote the question. There is a reason for downvoting? I am willing to improve the question if you give me your reasons. – hdkrus Sep 02 '14 at 22:29
  • @hdkrus: Okay, but *why* is that a problem? – Ry- Sep 02 '14 at 22:55
  • How exactly do you sign it? Do you really need to parse it at all? If you need the response to exactly match the request, then don't load it into any xml classes, treat it as a string (which otherwise I would never recommend). – Samuel Neff Sep 02 '14 at 23:35
  • @SamuelNeff: Ok, I'll explain, I must create an XML that includes another XML, and after that sign with RSA all the resulting XML. The problem is the the other XML I must include has ' and the XmlDocument class returns it as ' so the signed document does not fits the original and is rejected by receptor. – hdkrus Sep 03 '14 at 13:23
  • @hdkrus, ok, thanks for the further details. I posed a solution for you below. Should work for what you need. – Samuel Neff Sep 03 '14 at 16:13

1 Answers1

1

For your specific requirements, don't use XmlDocument or any other XML parser to parse the original document.

Do use XmlDocument or any other XML-specific classes to create your new document, except put a placeholder where the original document needs to go, like ORIGINAL_DOCUMENT_HERE. Then after you've generated the resulting text XML for your new document, replace ORIGINAL_DOCUMENT_HERE with your original received text, and then sign the result.

Not a normal way to work with XML, but should work for your specific use case.

Samuel Neff
  • 73,278
  • 17
  • 138
  • 182
  • SamuelNeff, thanx for your reply, but the problem is that I use the .Net framework to perform the signing and it uses underneath the XmlDocument anyway, so I'll need another signing library too. It becomes a big deal because the project I'm working is old and full of XmlDocument usage so I would need to change a great amount of code that works fine(except by the entities of course). – hdkrus Sep 04 '14 at 18:01
  • @hdkrus, Sorry to hear that. Legacy code is always fun. – Samuel Neff Sep 04 '14 at 19:33