C#操作XML

XmlDocument xml = new XmlDocument();

XmlDeclaration dec = xml.CreateXmlDeclaration("1.0", null, null);
xml.AppendChild(dec);

XmlComment com = xml.CreateComment("这是注释");
xml.AppendChild(com);

XmlElement root = xml.CreateElement("symbol");
root.InnerText = "root";
xml.AppendChild(root);

XmlElement symbol = xml.CreateElement("symbol");
symbol.InnerText = "InnerText";
symbol.SetAttribute("ATT", "Attribute");
symbol.SetAttribute("ATT2", "Attribute2");
xml.DocumentElement.AppendChild(symbol);

XmlTextWriter xw = new XmlTextWriter("d:\\test.xml", null);
xw.Formatting = Formatting.Indented;
xml.WriteContentTo(xw);
xw.Close();

posted on 2012-07-09 21:14  月生  阅读(127)  评论(0编辑  收藏  举报