Display an XML file to Console in C#

How to display an XML file to Console

Recently, I got a question in an email asking how to load an XML file and display it on the system console.

We can use XmlDocument to read an XML document. The Load method is used to load an XML into an XmlDocument. Once a document is loaded, we can use the Save method to display its content to the console. The key is, passing Console.Out as a parameter.

XmlDocument xmlDoc = new XmlDocument();

string filename = @"C:\Books\Books.xml";           

xmlDoc.Load(filename);

xmlDoc.Save(Console.Out);

You must import the System.Xml namespace in your code. 

Up Next
    Ebook Download
    View all
    Learn
    View all