Csharp/C Sharp/XML/XML Create
Append Child
<source lang="csharp">
using System; using System.Xml; class MainClass {
public static void Main() { 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("Tea Pot")); productNode.AppendChild(nameNode); priceNode = doc.CreateElement("productPrice"); priceNode.AppendChild(doc.CreateTextNode("12.99")); productNode.AppendChild(priceNode); doc.Save(Console.Out); }
}
</source>
Create XML document: xml element and properties
<source lang="csharp"> using System; using System.Xml;
class XMLDemo{ [STAThread] static void Main(string[] args) { XmlDocument xmlDom = new XmlDocument(); xmlDom.AppendChild(xmlDom.CreateElement("", "books", "")); XmlElement xmlRoot = xmlDom.DocumentElement; XmlElement xmlBook; XmlElement xmlTitle, xmlAuthor, xmlPrice; XmlText xmlText; xmlBook= xmlDom.CreateElement("", "A", ""); xmlBook.SetAttribute("property", "", "a"); xmlTitle = xmlDom.CreateElement("", "B", ""); xmlText = xmlDom.CreateTextNode("text"); xmlTitle.AppendChild(xmlText); xmlBook.AppendChild(xmlTitle); xmlRoot.AppendChild(xmlBook); xmlAuthor = xmlDom.CreateElement("", "C", ""); xmlText = xmlDom.CreateTextNode("textg"); xmlAuthor.AppendChild(xmlText); xmlBook.AppendChild(xmlAuthor); xmlPrice = xmlDom.CreateElement("", "D", ""); xmlText = xmlDom.CreateTextNode("99999"); xmlPrice.AppendChild(xmlText); xmlBook.AppendChild(xmlPrice); xmlRoot.AppendChild(xmlBook); Console.WriteLine(xmlDom.InnerXml); xmlDom.Save("books.xml"); XmlDocument xmlDom2 = new XmlDocument(); xmlDom2.Load("books.xml"); Console.WriteLine(xmlDom2.InnerXml); } }
</source>
Programmatically creating a new XML document.
<source lang="csharp"> using System; using System.Xml; public class GenerateXml {
private static void Main() { 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 = "01"; productNode.Attributes.Append(productAttribute); productsNode.AppendChild(productNode); XmlNode nameNode = doc.CreateElement("Name"); nameNode.AppendChild(doc.CreateTextNode("Java")); productNode.AppendChild(nameNode); XmlNode priceNode = doc.CreateElement("Price"); priceNode.AppendChild(doc.CreateTextNode("Free")); productNode.AppendChild(priceNode); // Create and add another product node. productNode = doc.CreateElement("product"); productAttribute = doc.CreateAttribute("id"); productAttribute.Value = "02"; productNode.Attributes.Append(productAttribute); productsNode.AppendChild(productNode); nameNode = doc.CreateElement("Name"); nameNode.AppendChild(doc.CreateTextNode("C#")); productNode.AppendChild(nameNode); priceNode = doc.CreateElement("Price"); priceNode.AppendChild(doc.CreateTextNode("Free")); productNode.AppendChild(priceNode); doc.Save(Console.Out); }
}
</source>