CSharp examples for XML:XML Document
Insert Nodes in an XML Document
using System;//from w w w.j av a 2s. c o m using System.Xml; public class GenerateXml { [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("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 = "1002"; productNode.Attributes.Append(productAttribute); productsNode.AppendChild(productNode); nameNode = doc.CreateElement("productName"); nameNode.AppendChild(doc.CreateTextNode("test test")); productNode.AppendChild(nameNode); priceNode = doc.CreateElement("productPrice"); priceNode.AppendChild(doc.CreateTextNode("102.99")); productNode.AppendChild(priceNode); doc.Save(Console.Out); } }