c# – Convert XmlDocument to String

c# – Convert XmlDocument to String

Assuming xmlDoc is an XmlDocument object whats wrong with xmlDoc.OuterXml?

n

return xmlDoc.OuterXml;n

n

The OuterXml property returns a string version of the xml.

There arent any quotes. Its just VS debugger. Try printing to the console or saving to a file and youll see. As a side note: always dispose disposable objects:

n

using (var stringWriter = new StringWriter())nusing (var xmlTextWriter = XmlWriter.Create(stringWriter))n{n    xmlDoc.WriteTo(xmlTextWriter);n    xmlTextWriter.Flush();n    return stringWriter.GetStringBuilder().ToString();n}n

c# – Convert XmlDocument to String

If you are using Windows.Data.Xml.Dom.XmlDocument version of XmlDocument (used in UWP apps for example), you can use yourXmlDocument.GetXml() to get the XML as a string.

Leave a Reply

Your email address will not be published.