C# XML操作之一:使用XmlDocument來讀寫


 所有代碼都在同一個類中,含有對象

XmlDocument doc = new XmlDocument();

新建XML,並且寫入內容

        private void button4_Click(object sender, EventArgs e)
        {
            
            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();
        }

創建的xml文件內容為

 

往已有XML文件中添加內容

原有books.xml內容如下

 

<?xml version="1.0" encoding="ISO-8859-1"?>
<!--  Copyright w3school.com.cn -->
<!-- W3School.com.cn bookstore example -->
<bookstore>
<book category="children">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="cooking">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="web" cover="paperback">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
<book category="web">
<title lang="en">XQuery Kick Start</title>
<author>James McGovern</author>
<author>Per Bothner</author>
<author>Kurt Cagle</author>
<author>James Linn</author>
<author>Vaidyanathan Nagarajan</author>
<year>2003</year>
<price>49.99</price>
</book>
</bookstore>
View Code

操作代碼

       doc.Load("books.xml");
            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("Authooooor");
            newAuthor.InnerText = "James Lorain";
            newbook.AppendChild(newAuthor);

            doc.DocumentElement.AppendChild(newbook);
            XmlTextWriter tr = new XmlTextWriter("newbook.xml", null);
            tr.Formatting = Formatting.Indented;
            doc.WriteContentTo(tr);
            tr.Close();

結果就是在原來bookstore節點下附加了新的子節點

 2種方法搜索所有title節點,並且打印其內容

效果

            doc.Load("books.xml");
            XmlNodeList nodeList = doc.GetElementsByTagName("title");
        //下面代碼效果完全相同
       XmlNodeList nodeList2 = doc.SelectNodes("/bookstore/book/title");
foreach (XmlNode node in nodeList) { content.Items.Add(node.OuterXml); }

 

 

https://blog.csdn.net/sony0732/article/details/2301958

https://www.cnblogs.com/zhangyf/archive/2009/06/03/1495459.html

https://www.cnblogs.com/malin/archive/2010/03/04/1678352.html

https://www.cnblogs.com/a1656344531/archive/2012/11/28/2792863.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM