Generate XML Document with attributes : Xml Creation : XML C# Examples


C# Examples » XML » Xml Creation »

 

Generate XML Document with attributes









    
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);

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


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



Output

Pot102.99


HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo XML
» Xml Creation