使用XmlReader遍歷文件是很方便的,使用XmlWriter產生一個新的XML文件也很容易.但是對現有的XML進行修改,例如添加一個元素或修改一個屬性值,就比較麻煩了.此時,可以使用XmlDocument物件,透過呼叫DOM方法修改文件,然後再儲存.由於DOM已經進行了標準化,很多語言都對他進行了支持,比如JS,因此這裡的很多方法與JS中都是一致的,比如GetElementByID(),GetElementByTagName(),App endChild(),InsertAfter()等.
1、建立一個xml檔案
XmlDocument doc = new XmlDocument(); XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "UTF-8", null); doc.AppendChild(dec); XmlElement root = doc.CreateElement("bookstore");//创建根节点 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、插入節點與建立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節點
將genre屬性值為“novel“的節點的genre值改為“updatenovel”,將該節點的子節點的文字修改為“啦啦啦啦”。
XmlNodeList nodeList=xmlDoc.SelectSingleNode("bookstore").ChildNodes;//获取bookstore节点的所有子节点 foreach(XmlNode xn in nodeList)//遍历所有子节点 { XmlElement xe=(XmlElement)xn;//将子节点类型转换为XmlElement类型 if(xe.GetAttribute("genre")=="novel")//如果genre属性值为“李赞红” { xe.SetAttribute("genre","updatenovel");//则修改该属性为“update李赞红” XmlNodeList nls=xe.ChildNodes;//继续获取xe子节点的所有子节点 foreach(XmlNode xn1 in nls)//遍历 { XmlElement xe2=(XmlElement)xn1;//转换类型 if(xe2.Name=="title")//如果找到 { xe2.InnerText="亚胜";//则修改 break;//找到退出来就可以了 } } break; } } xmlDoc.Save("bookstore.xml");//保存。
4、刪除節點
節點的genre屬性,刪除 節點。
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();//删除该节点的全部内容 } } 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 物件操作詳細步驟的詳細內容。更多資訊請關注PHP中文網其他相關文章!

XML在RSS中具有結構化數據、可擴展性、跨平台兼容性和解析驗證的優勢。 1)結構化數據確保內容的一致性和可靠性;2)可擴展性允許添加自定義標籤以適應內容需求;3)跨平台兼容性使其在不同設備上無縫工作;4)解析和驗證工具確保Feed的質量和完整性。

RSS在XML中的實現方式是通過結構化的XML格式來組織內容。 1)RSS使用XML作為數據交換格式,包含頻道信息和項目列表等元素。 2)生成RSS文件需按規範組織內容,發佈到服務器供訂閱。 3)RSS文件可通過閱讀器或插件訂閱,實現內容自動更新。

RSS的高級功能包括內容命名空間、擴展模塊和條件訂閱。 1)內容命名空間擴展RSS功能,2)擴展模塊如DublinCore或iTunes添加元數據,3)條件訂閱根據特定條件篩選條目。這些功能通過添加XML元素和屬性實現,提升信息獲取效率。

RSSFEEDSUSEXMLTOSSTRUCTURECONTUPDATE.1)XMLPROVIDEDIDESAHIERARCHICALSTRUCTUREFFORDATA.2)THEELEMENTDEFINESTHEEFEED'SIDENTITYANDCONTAINS ELEMENT.3)ELEMENTEMERPREPRESERPRESENTERPRESENTIVIDIVIVELPIECTUALPIECES.4)RSSSSSSSSSSSISEXTEXTENSIBLERECTICERSINCREECTINCERINCTICENT.5)

RSS和XML是用於網絡內容管理的工具。 RSS用於發布和訂閱內容,XML用於存儲和傳輸數據。它們的工作原理包括內容髮布、訂閱和更新推送。使用示例包括RSS發布博客文章和XML存儲書籍信息。

RSS文檔是基於XML的結構化文件,用於發布和訂閱頻繁更新的內容。它的主要作用包括:1)自動化內容更新,2)內容聚合,3)提高瀏覽效率。通過RSSfeed,用戶可以訂閱並及時獲取來自不同來源的最新信息。

RSS的XML結構包括:1.XML聲明和RSS版本,2.頻道(Channel),3.條目(Item)。這些部分構成了RSS文件的基礎,允許用戶通過解析XML數據來獲取和處理內容信息。

RSSFEEDSUSEXMLTOSYNDICATECONTENT; PARSINGTHEMINVOLVESLOADINGINGINGINGINSSTRUCTURE,andExtractingData.ApplicationsIncludeBuildBuildingNewSagGregatorSaterNewSagGregatorSator andTrackingPodcastepodcastepisodes。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

禪工作室 13.0.1
強大的PHP整合開發環境

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

SublimeText3漢化版
中文版,非常好用