Write XML to Console Using C#

The XmlWriter class is an abstract base class that writes fast, forward-only, non-cached XML streams. The XmlWriter class also validates and verifies element and attribute names and ensures that the generated XML is well-formed. 

The XmlWriter.Create static method creates the XML. The Create method has several 
overloaded forms and can save the generated XML into various objects.
We can use the Console.Out parameter with the XmlWriter.Create method to spit 
the XML out to the console.  
XmlWriter writer = XmlWriter.Create(Console.Out); 
The following code snippet creates an XML and spits it out to the console.

using (XmlWriter writer = XmlWriter.Create(Console.Out))
{

    // Write Processing Instruction
    String pi = "type=\"text/xsl\" href=\"book.xsl\"";
    writer.WriteProcessingInstruction("xml-stylesheet", pi);

    // Write DocumentType
    writer.WriteDocType("book", null, null, "<!ENTITY h \"hardcover\">");

    // Write a Comment

    writer.WriteComment("This is a book sample XML");

    // Root element - start tag
    writer.WriteStartElement("book");

    // Write ISBN attribute
    writer.WriteAttributeString("ISBN", "9831123212");

    // Write year attribute
    writer.WriteAttributeString("yearpublished", "2002"); 

    // Write title
    writer.WriteElementString("author", "Mahesh Chand");

    // Write author
    writer.WriteElementString("title", "Visual C# Programming");

    // Write price
    writer.WriteElementString("price", "44.95");

    // Write CDATA
    writer.WriteCData("APress Book!");

    // Root element - end tag
    writer.WriteEndElement();

    // End Documentd
    writer.WriteEndDocument();

    // Flush it
    writer.Flush();

}