Convert XPathDocument to String

I have an XPathDocument and you want to export it to a string containing the document as an XML representation. What is the easiest way to do this?

+3
source share
2 answers

To get a string representation of an XML document, you can do the following:

XPathDocument xdoc = new XPathDocument(@"C:\samples\sampleDocument.xml");
string xml = xdoc.CreateNavigator().OuterXml;

If you want your string to contain a complete representation of an XML document, including an XML declaration, you can use the following code:

XPathDocument xdoc = new XPathDocument(@"C:\samples\sampleDocument.xml");
StringBuilder sb = new StringBuilder();
using (XmlWriter xmlWriter = XmlWriter.Create(sb))
{
    xdoc.CreateNavigator().WriteSubtree(xmlWriter);
}
string xml = sb.ToString();
+13
source

An XPathDocumentis a read-only representation of an XML document. This means that the internal representation will not change. To get the XML, you can get the source document.

0xA3, ( , , , XDM)

0

Source: https://habr.com/ru/post/1760255/


All Articles