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();
source
share