Save, AppendChild, CreateXmlDeclaration, CreateElement

   
  
using System;
using System.Xml;

class MainClass {
    public static void Main() {
        XmlDocument doc = new XmlDocument();
        XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
        doc.AppendChild(docNode);

        XmlNode productsNode = doc.CreateElement("products");
        doc.AppendChild(productsNode);

        XmlNode productNode = doc.CreateElement("product");
        XmlAttribute productAttribute = doc.CreateAttribute("id");
        productAttribute.Value = "1";
        productNode.Attributes.Append(productAttribute);
        productsNode.AppendChild(productNode);

        XmlNode nameNode = doc.CreateElement("productName");
        nameNode.AppendChild(doc.CreateTextNode("Coffee"));
        productNode.AppendChild(nameNode);
        XmlNode priceNode = doc.CreateElement("productPrice");
        priceNode.AppendChild(doc.CreateTextNode("0.99"));
        productNode.AppendChild(priceNode);

        productNode = doc.CreateElement("product");
        productAttribute = doc.CreateAttribute("id");
        productAttribute.Value = "2";
        productNode.Attributes.Append(productAttribute);
        productsNode.AppendChild(productNode);
        nameNode = doc.CreateElement("productName");
        nameNode.AppendChild(doc.CreateTextNode("Tea Pot"));
        productNode.AppendChild(nameNode);
        priceNode = doc.CreateElement("productPrice");
        priceNode.AppendChild(doc.CreateTextNode("12.99"));
        productNode.AppendChild(priceNode);

        doc.Save(Console.Out);
    }
}

   
     


LoadXml

   
  

using System;
using System.IO;
using System.Xml;
public class BookListing {
    public static void Main() {
        XmlDocument doc = new XmlDocument();
        String entry = "<book genre=&#039;biography&#039;" +
         " ISBN=&#039;1111111111&#039;><title>my title</title>" +
         "</book>";
        doc.LoadXml(entry);

        StringWriter writer = new StringWriter();
        doc.Save(writer); // to StringWriter
        String strXML = writer.ToString(); // to String
        Console.WriteLine(strXML);
    }
}

   
     


A Simple XML Example

   
  

using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Linq;
public class MainClass {
    public static void Main() {
        XmlElement xmlBook;
        XmlAttribute xmlParticipantType;
        XmlElement xmlFirstName;
        XmlElement xmlLastName;

        XmlDocument xmlDoc = new XmlDocument();
        XmlElement xmlBooks = xmlDoc.CreateElement("Books");
        xmlDoc.AppendChild(xmlBooks);

        xmlBook = xmlDoc.CreateElement("Book");

        xmlParticipantType = xmlDoc.CreateAttribute("type");
        xmlParticipantType.InnerText = "Author";
        xmlBook.Attributes.Append(xmlParticipantType);

        xmlFirstName = xmlDoc.CreateElement("FirstName");
        xmlFirstName.InnerText = "J";
        xmlBook.AppendChild(xmlFirstName);

        xmlLastName = xmlDoc.CreateElement("LastName");
        xmlLastName.InnerText = "R";
        xmlBook.AppendChild(xmlLastName);

        xmlBooks.AppendChild(xmlBook);

        xmlBook = xmlDoc.CreateElement("Book");

        xmlParticipantType = xmlDoc.CreateAttribute("type");
        xmlParticipantType.InnerText = "Author";
        xmlBook.Attributes.Append(xmlParticipantType);

        xmlFirstName = xmlDoc.CreateElement("FirstName");
        xmlFirstName.InnerText = "E";
        xmlBook.AppendChild(xmlFirstName);

        xmlLastName = xmlDoc.CreateElement("LastName");
        xmlLastName.InnerText = "B";
        xmlBook.AppendChild(xmlLastName);

        xmlBooks.AppendChild(xmlBook);

        XmlNodeList authorsList = xmlDoc.SelectNodes("Books/Book[@type="Author"]");

        foreach (XmlNode node in authorsList) {
            XmlNode firstName = node.SelectSingleNode("FirstName");
            XmlNode lastName = node.SelectSingleNode("LastName");
            Console.WriteLine("{0} {1}", firstName, lastName);
        }
    }
}

   
     


XML Write: comment, start element, end element, attribute

   

using System;
using System.Xml;

public class TestXMLWriter{
  static void Main(string[] args)
  {
    XmlTextWriter writer = new XmlTextWriter("test.xml", new System.Text.ASCIIEncoding());
    writer.Formatting = Formatting.Indented;
    writer.Indentation = 4;
    writer.WriteStartDocument();
    writer.WriteComment("Comment");
    writer.WriteStartElement("ElementName", "myns");
    writer.WriteStartAttribute("prefix", "attrName", "myns");
    writer.WriteEndAttribute();
    writer.WriteElementString("ElementName", "myns", "value");
    writer.WriteEndElement();
    writer.WriteEndDocument();
    writer.Flush();
    writer.Close();
  }
}



           
          


Write XML document to console

   

using System;
using System.Xml;

public class HelloXML {
  public static void Main(string [] args) {
    XmlTextWriter writer = new XmlTextWriter(Console.Out);
    writer.WriteStartDocument( );
    writer.WriteElementString("www.kutayzorlu.com/java2s/com", "XML");
    writer.WriteEndDocument( );
    writer.Close( );
  }
}

           
          


Illustrates the XmlTextWriter class

   

/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy

Publisher: Sybex;
ISBN: 0782129110
*/


/*
  Example20_1.cs illustrates the XmlTextWriter class
*/

using System;
using System.Xml;

public class Example20_1 
{

    public static void Main() 
    {

        // use the XmlTextWriter to open a new XML file
        XmlTextWriter xw = new XmlTextWriter(@"Cust4.xml", 
            System.Text.Encoding.UTF8);

        // write the document declaration
        xw.WriteStartDocument();

        // write the first element
        xw.WriteStartElement("NewDataSet");

        // write the first customer
        xw.WriteStartElement("Customers");
        xw.WriteElementString("CustomerID", "ALFKI");
        xw.WriteElementString("CompanyName", "Alfreds Futterkiste");
        xw.WriteElementString("ContactName", "Maria Anders");
        xw.WriteElementString("ContactTitle", "Sales Representative");
        xw.WriteElementString("Address", "Obere Str. 57");
        xw.WriteElementString("City", "Berlin");
        xw.WriteElementString("PostalCode", "12209");
        xw.WriteElementString("Country", "Germany");
        xw.WriteElementString("Phone", "030-0074321");
        xw.WriteElementString("Fax", "030-0076545");
        xw.WriteEndElement();

        // write the second customer
        xw.WriteStartElement("Customers");
        xw.WriteElementString("CustomerID", "BONAPP");
        xw.WriteElementString("CompanyName", "Bon App&#039;");
        xw.WriteElementString("ContactName", "Laurence Lebihan");
        xw.WriteElementString("ContactTitle", "Owner");
        xw.WriteElementString("Address", "12, rue des Bouchers");
        xw.WriteElementString("City", "Marseille");
        xw.WriteElementString("PostalCode", "13008");
        xw.WriteElementString("Country", "France");
        xw.WriteElementString("Phone", "91.24.45.40");
        xw.WriteElementString("Fax", "91.24.45.41");
        xw.WriteEndElement();

        // end the NewDataSet element
        xw.WriteEndElement();

        // end the document
        xw.WriteEndDocument();

        // flush and close
        xw.Flush();
        xw.Close();
    }

}

/**/

           
          


Write To XML File

   

/*
 * C# Programmers Pocket Consultant
 * Author: Gregory S. MacBeth
 * Email: gmacbeth@comporium.net
 * Create Date: June 27, 2003
 * Last Modified Date:
 * Version: 1
 */
using System;
using System.IO;
using System.Xml;
using System.Xml.Schema;


namespace Client.Chapter_22___XML
{
  public class WriteToXMLFile
  {
    static void Main(string[] args)
    {
      string document = "newbooks.xml";
      XmlTextWriter myXmlTextWriter = null;
      XmlTextReader myXmlTextReader = null;

      myXmlTextWriter = new XmlTextWriter(args[1], null);
      myXmlTextWriter.Formatting = Formatting.Indented;
      myXmlTextWriter.WriteStartDocument(false);
      myXmlTextWriter.WriteDocType("bookstore", null, "books.dtd", null);
      myXmlTextWriter.WriteComment("This file represents another fragment of a book store inventory database");
      myXmlTextWriter.WriteStartElement("bookstore");
      myXmlTextWriter.WriteStartElement("book", null);
      myXmlTextWriter.WriteAttributeString("genre", "autobiography");
      myXmlTextWriter.WriteAttributeString("publicationdate", "1979");
      myXmlTextWriter.WriteAttributeString("ISBN", "0-7356-0562-9");
      myXmlTextWriter.WriteElementString("title", null, "The Autobiography of Mark Twain");
      myXmlTextWriter.WriteStartElement("Author", null);
      myXmlTextWriter.WriteElementString("first-name", "Mark");
      myXmlTextWriter.WriteElementString("last-name", "Twain");
      myXmlTextWriter.WriteEndElement();
      myXmlTextWriter.WriteElementString("price", "7.99");
      myXmlTextWriter.WriteEndElement();
      myXmlTextWriter.WriteEndElement();

      //Write the XML to file and close the writer
      myXmlTextWriter.Flush();
      myXmlTextWriter.Close();
      if (myXmlTextWriter != null)
        myXmlTextWriter.Close();
    }
  }

}