C#读取与修改XML文档
生活随笔
收集整理的這篇文章主要介紹了
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文档的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C#通过Outlook发送邮件
- 下一篇: c# 5.0入门经典笔记