Here is how I'm currently converting XMLDocument to String
StringWriter stringWriter = new StringWriter();
XmlTextWriter xmlTextWriter = new XmlTextWriter(stringWriter);
xmlDoc.WriteTo(xmlTextWriter);
return stringWriter.ToString();
The problem with this method is that if I have " ((quotes) which I have in attributes) it escapes them.
For Instance:
<Campaign name="ABC">
</Campaign>
Above is the expected XML. But it returns
<Campaign name=\"ABC\">
</Campaign>
I can do String.Replace "\" but is that method okay? Are there any side-effects? Will it work fine if the XML itself contains a "\"
As an extension method:
Now to use simply:
If you are using
Windows.Data.Xml.Dom.XmlDocument
version ofXmlDocument
(used in UWP apps for example), you can useyourXmlDocument.GetXml()
to get the XML as a string.There aren't any quotes. It's just VS debugger. Try printing to the console or saving to a file and you'll see. As a side note: always dispose disposable objects:
Assuming xmlDoc is an XmlDocument object whats wrong with xmlDoc.OuterXml?
The OuterXml property returns a string version of the xml.
"
is shown as\"
in the debugger, but the data is correct in the string, and you don't need to replace anything. Try to dump your string to a file and you will note that the string is correct.