Generate Xml Document : Xml Creation : XML C# Examples


C# Examples » XML » Xml Creation »

 

Generate Xml Document









    
using  System;
using  System.Xml;

public  class  MainClass
{
    [STAThread]
    private  static  void  Main(string[]  args)
    {
        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  =  "1001";
        productNode.Attributes.Append(productAttribute);
                productsNode.AppendChild(productNode);

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


            //  Save  the  document  (to  the  Console  window  rather  than  a  file).
        doc.Save(Console.Out);
    }
}
    
   
  
   



Output

Gourmet Coffee0.99


HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo XML
» Xml Creation