C# Print Any Complete XML Document to Console?
Anyone have a snippet?
(Hides behind the cubicle...shit.)
C# Print Any Complete XML Document to Console?
Anyone have a snippet?
(Hides behind the cubicle...shit.)
Are you just looking for:
Console.WriteLine(myXmlDoc.OuterXml); // Using System.Xml.XmlDocument
I know this an old post but FWIW: Here's how you can "pretty print" your XML ...
public static String PrettyPrintXML(String sourceXML, Encoding encoding)
{
using (MemoryStream memory = new MemoryStream())
{
using (XmlTextWriter xmlWriter = new XmlTextWriter(memory, encoding))
{
XmlDocument document = new XmlDocument();
document.LoadXml(sourceXml);
xmlWriter.Formatting = Formatting.Indented;
xmlWriter.Indentation = 4;
document.WriteContentTo(xmlWriter);
xmlWriter.Flush();
memory.Position = 0;
using (StreamReader reader = new StreamReader(memory))
{
return reader.ReadToEnd();
}
}
}
}
If you use an XDocument (in System.Xml.Linq)
for your XML you can just call ToString() on it for an indented version of the XML within.
XDocument doc = XDocument.Load(Server.MapPath("~/Data.xml"));
Console.WriteLine(doc.ToString());
If you don't want it indented then use the overload of ToString() and pass in a SaveOptions enumeration:
Console.WriteLine(doc.ToString(SaveOptions.DisableFormatting));