首页 > 其他 > 详细

LINQ to XML

时间:2021-04-07 12:53:12      阅读:17      评论:0      收藏:0      [点我收藏+]

创建XML文档

技术分享图片

 

 1.使用XMLDocument的方式

技术分享图片
            XmlDocument doc = new XmlDocument();
            doc.AppendChild(doc.CreateXmlDeclaration("1.0", "utf-8", null));
            XmlElement newbook = doc.CreateElement("book");
            newbook.SetAttribute("genre", "Mystery");
            newbook.SetAttribute("publicationdate", "2001");
            newbook.SetAttribute("ISBN", "123345525");

            XmlElement newTitle = doc.CreateElement("title");
            newTitle.InnerText = "The Case of The missing cookie";
            newbook.AppendChild(newTitle);

            XmlElement newAuthor = doc.CreateElement("Author");
            newAuthor.InnerText = "James Lorain";
            newbook.AppendChild(newAuthor);
            if (doc.DocumentElement == null)
                doc.AppendChild(newbook);
            XmlTextWriter tr = new XmlTextWriter("newbook.xml", Encoding.UTF8);
            tr.Formatting = Formatting.Indented;
            doc.WriteContentTo(tr);
            tr.Close();
View Code

2.使用XDocument的方式

 

            XDocument xdoc = new XDocument();
            XElement root = new XElement("Book");
            xdoc.Add(root);

            XAttribute genre = new XAttribute("genre", "Mystery");
            XAttribute date = new XAttribute("publicationdate", "2001");
            XAttribute isbn = new XAttribute("ISBN", "123345525");
            root.Add(genre, date, isbn);

            XElement title = new XElement("title");
            title.Value = "The Case of The missing cookie";

            XElement author = new XElement("Author", "James Lorain");
            root.Add(title, author);

            xdoc.Save("xdoc.xml");

 

LINQ to XML

原文:https://www.cnblogs.com/noigel/p/14626738.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!