Create XML document by specifying the elements : Xml Creation « XML « C# / CSharp Tutorial






using System;
using System.Collections;
using System.Data;
using System.Xml;

public class MainClass {
   public static void Main() {
      XmlDocument doc = new XmlDocument();

      XmlElement root = doc.CreateElement( "books" );
      doc.AppendChild( root );

      XmlElement eltBook = doc.CreateElement( "book" );
      root.AppendChild( eltBook );

      XmlElement eltTitle = doc.CreateElement( "title" );
      eltTitle.AppendChild( doc.CreateTextNode( "myTitle" ) );
      eltBook.AppendChild( eltTitle );

      XmlElement eltAuthor = doc.CreateElement( "author" );
      eltAuthor.AppendChild( doc.CreateTextNode(  "myAuthor" ) );
      eltBook.AppendChild( eltAuthor );

      XmlElement eltPrice = doc.CreateElement( "price" );
      eltPrice.AppendChild( doc.CreateTextNode(  "10.0" ) );
      eltBook.AppendChild( eltPrice );

      Console.WriteLine( doc.OuterXml );
   }
}
myTitlemyAuthor10.0








30.1.Xml Creation
30.1.1.Create XML document by specifying the elements
30.1.2.Generate Xml Document
30.1.3.Generate XML Document with attributes
30.1.4.Helper method for generating the Xml document
30.1.5.Adding an element to the document