溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

XmlDocument對象操作的示例分析

發(fā)布時間:2021-09-17 14:21:00 來源:億速云 閱讀:115 作者:小新 欄目:編程語言

這篇文章主要介紹XmlDocument對象操作的示例分析,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!

使用XmlReader遍歷文檔是很方便的,使用XmlWriter生成一個新的XML文檔也很容易.但是對現(xiàn)有的XML進行修改,例如添加一個元素或修改一個屬性值,就比較麻煩了.此時,可以使用XmlDocument對象,通過調用DOM方法對文檔進行修改,然后再保存.由于DOM已經進行了標準化,很多語言都對他進行了支持,比如JS,因此這里的很多方法與JS中都是一致的,比如GetElementByID(),GetElementByTagName(),AppendChild(),InsertAfter()等.

1、創(chuàng)建一個xml文件

XmlDocument doc = new XmlDocument();
XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
doc.AppendChild(dec);
 XmlElement root = doc.CreateElement("bookstore");//創(chuàng)建根節(jié)點
doc.AppendChild(root);
 XmlElement newBook = _doc.CreateElement("book");//create a new 'book' element
//set some attributes
newBook.SetAttribute("genre", "Mystery");      newBook.SetAttribute("publicationdate", "2001");      newBook.SetAttribute("ISBN", "123456789");
//create a new 'title' element
XmlElement newTitle = _doc.CreateElement("title");      newTitle.InnerText = "Case of the Missing Cookie";      newBook.AppendChild(newTitle);
 //create new author element
XmlElement newAuthor = _doc.CreateElement("author");      newBook.AppendChild(newAuthor);
//create new name element      XmlElement newName = _doc.CreateElement("name");      newName.InnerText = "Cookie Monster";      newAuthor.AppendChild(newName);
     //create new price element      XmlElement newPrice = _doc.CreateElement("price");      newPrice.InnerText = "9.95";      newBook.AppendChild(newPrice);      //add to the current documentdoc.DocumentElement.AppendChild(newBook);//_doc.DocumentElement為獲取xml的根
doc.Save("bb.xml");將 XML 文檔保存到指定的位置

2、插入節(jié)點 與創(chuàng)建xml 文件類似

_doc.Load("books.xml");      XmlElement newBook = _doc.CreateElement("book");      newBook.SetAttribute("genre", "Mystery");      newBook.SetAttribute("publicationdate", "2001");      newBook.SetAttribute("ISBN", "123456789");      XmlElement newTitle = _doc.CreateElement("title");      newTitle.InnerText = "Case of the Missing Cookie";      newBook.AppendChild(newTitle);      XmlElement newAuthor = _doc.CreateElement("author");      newBook.AppendChild(newAuthor);      XmlElement newName = _doc.CreateElement("name");      newName.InnerText = "Cookie Monster";      newAuthor.AppendChild(newName);      XmlElement newPrice = _doc.CreateElement("price");      newPrice.InnerText = "9.95";      newBook.AppendChild(newPrice);      _doc.DocumentElement.AppendChild(newBook);
_doc.Save("booksEdit.xml");
或者下面這樣保存      XmlTextWriter tr = new XmlTextWriter("booksEdit.xml", null);//將xml文檔保存,如果存在此文件,則覆蓋      tr.Formatting = Formatting.Indented;      _doc.WriteContentTo(tr);
tr.Close();

3、修改xml節(jié)點

將genre屬性值為“novel“的節(jié)點的genre值改為“updatenovel”,將該節(jié)點的子節(jié)點的文本修改為“啦啦啦啦”。

XmlNodeList nodeList=xmlDoc.SelectSingleNode("bookstore").ChildNodes;//獲取bookstore節(jié)點的所有子節(jié)點 foreach(XmlNode xn in nodeList)//遍歷所有子節(jié)點 { XmlElement xe=(XmlElement)xn;//將子節(jié)點類型轉換為XmlElement類型 if(xe.GetAttribute("genre")=="novel")//如果genre屬性值為“李贊紅” { xe.SetAttribute("genre","updatenovel");//則修改該屬性為“update李贊紅” XmlNodeList nls=xe.ChildNodes;//繼續(xù)獲取xe子節(jié)點的所有子節(jié)點 foreach(XmlNode xn1 in nls)//遍歷 { XmlElement xe2=(XmlElement)xn1;//轉換類型 if(xe2.Name=="title")//如果找到 { xe2.InnerText="亞勝";//則修改 break;//找到退出來就可以了 } } break; } } 
xmlDoc.Save("bookstore.xml");//保存。

4、刪除節(jié)點

節(jié)點的genre屬性,刪除 節(jié)點。

XmlNodeList xnl=xmlDoc.SelectSingleNode("bookstore").ChildNodes; 
foreach(XmlNode xn in xnl) 
{ 
XmlElement xe=(XmlElement)xn; 
if(xe.GetAttribute("genre")=="fantasy") 
{ 
xe.RemoveAttribute("genre");//刪除genre屬性 
} 
else if(xe.GetAttribute("genre")=="update李贊紅") 
{ 
xe.RemoveAll();//刪除該節(jié)點的全部內容 
} 
} 
xmlDoc.Save("bookstore.xml");

5、遍歷xml

  •  string filePath = "bookstore.xml";  
        XmlDocument doc = new XmlDocument();  
        doc.Load(filePath);  
        XmlNode root = doc.DocumentElement;  
        showNode(root);  
    private static void showNode(XmlNode root)  
    {  
        if (root.NodeType==XmlNodeType.Text)  
        {  
            Console.WriteLine(root.Value);  
        }  
        if (root.NodeType==XmlNodeType.Element)  
        {  
            Console.WriteLine(root.Name);  
        }  
        if (root.Attributes!=null&&root.Attributes.Count>0)  
        {  
            foreach (XmlAttribute attr  in root.Attributes)  
            {  
                Console.Write("{0}={1} ",attr.Name,attr.Value);  
            }  
            Console.WriteLine();  
        }  
        XmlNodeList chiledList = root.ChildNodes;  
        foreach (XmlNode child in chiledList)  
        {  
            showNode(child);  
        }  
    }

以上是“XmlDocument對象操作的示例分析”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI