I am seeing a difference between the way XmlDocument Load( ) and LoadXml( ) work in .NET 2.0. Given the XML document below (note the use of a right quote, ascii code 146):
<?xml version="1.0" encoding="utf-8"?>
<nodes>
<node>Some Data ’</node>
</nodes>
Why does it load fine with LoadXml( ) when passed in as a string but fails if passed in to Load( ) as a document. I.E. and other XML editors also will not load and display this file.
Simplified Code Example:
[WebMethod]
public bool SubmitData(string xmlDoc)
{
try
{
XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlDoc);
}
catch
{
return false;
}
return true;
}
I know the code is poor but it is just meant to demonstrate the problem. If the string “xmlDoc” is not a legitimate xml document, then I am trying to get this to fail.
I can’t control the content of the XML sent to me. I just receive it and work with it through the web service. Apparently, the people calling it are copying and pasting data from a Word Doc. I didn’t design this either but I am stuck maintaining it. 🙂
The difference is the encoding. When loading from a file, utf-8 decoding is applied and your code 146 is probably not valid utf-8 in your case. LoadXml ignores the encoding because .Net strings do not need to be decoded. Therfore your special character is a valid character and everything is fine.