XmlDocument和XDocument
1 private void button1_Click(object sender, EventArgs e) 2 { 3 XmlDocument doc = new XmlDocument(); 4 XmlDeclaration xdc= doc.CreateXmlDeclaration("1.0", "utf-8", null); 5 XmlElement rootelement = doc.CreateElement("Nodes"); 6 XmlElement ele1 = doc.CreateElement("Node"); 7 ele1.InnerText = "hello"; 8 ele1.SetAttribute("ID", "1"); 9 rootelement.AppendChild(ele1); 10 doc.AppendChild(xdc); 11 doc.AppendChild(rootelement); 12 doc.Save("a.xml"); 13 } 14 15 private void button2_Click(object sender, EventArgs e) 16 { 17 XDocument doc = new XDocument( 18 new XElement("root", 19 new XAttribute("name", "value"), 20 new XElement("child", "text node"))); 21 doc.Save("b.xml"); 22 }