日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

C#读取与修改XML文档

發布時間:2025/3/20 C# 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C#读取与修改XML文档 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在項目開發中,對XML文檔的操作是很常用的,這里,簡單的說明讀取與修改XML文檔

XML文檔的格式

<?xml version="1.0" encoding="utf-8"?> <webinfo><web><webname>谷歌</webname><weburl>http://www.chinaz.com/web/2013/1211/3307662.shtml</weburl><sendusername>谷歌小明</sendusername><sendusermail>553325869@qq.com</sendusermail><mailtitle>谷歌無法訪問</mailtitle><mailcontent>谷歌無法訪問</mailcontent><httpcode>404</httpcode><httpresult>NotFound</httpresult><httpdescript>未找到,服務器找不到請求的網頁。</httpdescript><webstate>失敗</webstate><ismail>已發送</ismail></web> </webinfo>

定義一個實體類,實現數據的傳遞

using System; using System.Collections.Generic; using System.Linq; using System.Web;namespace WebDetection.Model {public class WebXMLmodel{public WebXMLmodel() { }public string webname { get; set; }public string weburl { get; set; }public string sendusername { get; set; }public string sendusermail { get; set; }public string mailtitle { get; set; }public string mailcontent { get; set; }public string httpcode { get; set; }public string httpresult { get; set; }public string httpdescription { get; set; }public string Webstate { get; set; }public string ismail { get; set; }} }

讀取XML文檔

#region 讀取xml/// <summary>/// 讀取xml/// </summary>/// <returns></returns>public static List<WebXMLmodel> readXML(){// 采用XmlDocument操作XMLXmlDocument doc = new XmlDocument();doc.Load(@"C:\Users\楠\Desktop\web\WebDetection\WebDetection\WebInfo\WebXML.xml");// 實例化實體類WebXMLmodel webxmlmode = new WebXMLmodel();// 返回泛型List<WebXMLmodel> webxmllist = new List<WebXMLmodel>();// 獲取根節點XmlNode root = doc.SelectSingleNode("webinfo");// 獲取根節點下的所有子節點XmlNodeList child = root.ChildNodes;// 循環遍歷讀取XML文件foreach (XmlNode children in child){// 得到web節點的所有子節點XmlNodeList childrens = children.ChildNodes;// 獲取每一個子節點的值webxmlmode.webname = childrens.Item(0).InnerText;webxmlmode.weburl = childrens.Item(1).InnerText;webxmlmode.sendusername = childrens.Item(2).InnerText;webxmlmode.sendusermail = childrens.Item(3).InnerText;webxmlmode.mailtitle = childrens.Item(4).InnerText;webxmlmode.mailcontent = childrens.Item(5).InnerText;webxmlmode.httpcode = childrens.Item(6).InnerText;webxmlmode.httpresult = childrens.Item(7).InnerText;webxmlmode.httpdescription = childrens.Item(8).InnerText;webxmlmode.Webstate = childrens.Item(9).InnerText;webxmlmode.ismail = childrens.Item(10).InnerText;webxmllist.Add(webxmlmode);}return webxmllist;}#endregion

修改XML文檔

#region 修改xml/// <summary>/// 修改xml/// </summary>/// <param name="list"></param>/// <returns></returns>public static bool writeXML(List<WebXMLmodel> list){// 采用XmlDocument操作XMLXmlDocument doc = new XmlDocument();doc.Load(@"C:\Users\楠\Desktop\web\WebDetection\WebDetection\WebInfo\WebXML.xml");// 獲取根節點XmlNode root = doc.SelectSingleNode("webinfo");// 獲取根節點下的所有子節點XmlNodeList child = root.ChildNodes;int count=list.Count();count=0;// 循環遍歷修改XML文件foreach (XmlNode children in child){// 得到web節點的所有子節點XmlNodeList childrens = children.ChildNodes;// 獲取每一個子節點的值childrens.Item(6).InnerText=list[count].httpcode;childrens.Item(7).InnerText = list[count].httpresult;childrens.Item(8).InnerText = list[count].httpdescription;childrens.Item(9).InnerText = list[count].Webstate;childrens.Item(10).InnerText = list[count].ismail;count=count+1;}// 保存修改doc.Save(@"C:\Users\楠\Desktop\web\WebDetection\WebDetection\WebInfo\WebXML.xml");return true;}#endregion

實現對XML文檔操作的方法有很多種,上面僅僅是其中一種實現方式。

總結

以上是生活随笔為你收集整理的C#读取与修改XML文档的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。