XmlDocument.CreateElement : XmlDocument « System.Xml « C# / C Sharp by API






XmlDocument.CreateElement

  


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

   
    
  








Related examples in the same category

1.new XmlDocument()
2.XmlDocument.AppendChild(XmlNode docNode)
3.XmlDocument.CreateComment(String comments)
4.XmlDocument.CreateXmlDeclaration
5.XmlDocument.LoadXml(String xmlString)
6.XmlDocument.Save()
7.XmlDocument.SelectNodes
8.XmlDocument.SelectSingleNode