创建xml文档

        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(Server.MapPath("XMLFile.xml"));
        XmlNode root = xmlDoc.SelectSingleNode("Books");
        XmlElement book = xmlDoc.CreateElement("Book");

        XmlElement title = xmlDoc.CreateElement("Title");
        title.InnerText = "XML";
        book.AppendChild(title);
        XmlAttribute attr = xmlDoc.CreateAttribute("attr");
        attr.Value = "111";
        title.Attributes.Append(attr);

        XmlElement isbn = xmlDoc.CreateElement("ISBN");
        isbn.InnerText = "333333";
        book.AppendChild(isbn);
        XmlElement author = xmlDoc.CreateElement("Author");
        author.InnerText = "snow";
        book.AppendChild(author);
        XmlElement price = xmlDoc.CreateElement("Price");
        price.InnerText = "120";
        price.SetAttribute("Unit", "FCKpd___0quot");
        book.AppendChild(price);
        root.AppendChild(book);
        xmlDoc.Save(Server.MapPath("XMLFile.xml"));

posted on 2010-10-21 23:50  citygs  阅读(141)  评论(0编辑  收藏  举报