View Code
1 XmlDocument doc = new XmlDocument(); 2 XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "GB2312", null); 3 doc.AppendChild(dec); 4 //创建一个根节点(一级) 5 XmlElement root = doc.CreateElement("First"); 6 doc.AppendChild(root); 7 //创建节点(二级) 8 XmlNode node = doc.CreateElement("Seconde"); 9 //创建节点(三级) 10 XmlElement element1 = doc.CreateElement("Third1"); 11 element1.SetAttribute("Name", "Sam"); 12 element1.SetAttribute("ID", "665"); 13 element1.InnerText = "Sam Comment"; 14 node.AppendChild(element1); 15 16 XmlElement element2 = doc.CreateElement("Third2"); 17 element2.SetAttribute("Name", "Round"); 18 element2.SetAttribute("ID", "678"); 19 element2.InnerText = "Round Comment"; 20 node.AppendChild(element2); 21 22 23 24 root.AppendChild(node); 25 doc.Save(@"d:\bb.xml"); 26 Console.Write(doc.OuterXml);