C#操作XML檔案執行個體匯總

來源:互聯網
上載者:User

標籤:margin   border   event   檔案的操作   bool   操作xml   reac   png   static   

針對XML檔案的操作是C#程式設計中非常常見的功能。本文即以執行個體展示了C#操作XML檔案的幾個常見的樣本。具體如下:


1.返回節點下標

public static XmlDocument getDoc(String path)//載入xml文檔{  XmlDocument doc = new XmlDocument();  doc.Load(path);  return doc;}/// <summary>/// 返回找到的節點下標/// </summary>/// <param name="path">xml檔案路徑</param>/// <param name="bname">書名</param>/// <returns></returns>public static int getPosition(String path,string node, String bname){  int i;  XmlDocument doc = new XmlDocument();  doc.Load(path);  XmlNodeList nodeList = doc.SelectSingleNode(node).ChildNodes;  for (i = 0; i < nodeList.Count; i++)  { if (nodeList[i].ChildNodes[0].InnerText == bname) {   return i; }  }  return -1;}


2.遍曆資料

/// <summary>/// 遍曆資料/// </summary>/// <param name="sender"></param>/// <param name="e"></param>protected void btnRead_Click(object sender, EventArgs e){  XmlDocument doc = getDoc("books.xml");//載入xml文檔,books檔案存放於  foreach (XmlElement root in doc.DocumentElement.ChildNodes)//遍曆根的子節點  { Response.Write("<script>document.write(‘<br/>‘);</script>"); Response.Write("<script>document.write(‘" + root.Name + "‘);</script>");//節點名root.Name book foreach (XmlElement item in root.ChildNodes) {   Response.Write("<script>document.write(‘" + item.Name + "‘+‘:‘+‘" + item.InnerText + "‘);</script>");//輸出節點名和文本節點的值   Response.Write("<script>document.write(‘<br/>‘);</script>"); }  }}


3.尋找

/// <summary>/// 尋找/// </summary>/// <param name="path">檔案路徑</param>/// <param name="node">節點</param>/// <param name="bname">尋找關鍵字</param>/// <returns>XmlNode</returns>public static XmlNode Find(string path,string node,string bname){  XmlDocument doc = new XmlDocument();  doc.Load(path);//載入xml文檔  XmlNodeList nodeList = doc.SelectSingleNode(node).ChildNodes;  int i = getPosition(path, node, bname);//  if (i >= 0) return nodeList[i];  else return null;}


4.刪除節點

/// <summary>/// 刪除元素、屬性/// </summary>/// <param name="path">檔案</param>/// <param name="node">指定節點的父節點</param>/// <param name="attribute">為空白時刪除節點,否則刪除屬性</param>/// <param name="bname"></param>public static void Delete(string path,string node,string attribute,string bname){  XmlDocument doc = new XmlDocument();  doc.Load(path);  XmlNode root = doc.SelectSingleNode(node);  XmlNodeList nodeList = doc.SelectSingleNode(node).ChildNodes;  int i = getPosition(path, node, bname);//返回指定的節點下標  if (i >= 0)  { if (attribute.Equals("")) {   root.RemoveChild(nodeList[i]); } else {   XmlElement xn = (XmlElement)nodeList[i];   xn.RemoveAttribute(attribute); }  }  doc.Save(path);}


5.添加

/// <summary>/// 添加元素值/// </summary>/// <param name="path"></param>/// <param name="node"></param>/// <param name="element"></param>/// <param name="value"></param>/// <param name="i">插入的下標,如果為負,預設從最後一個節點插入</param>/// <returns></returns>public static bool Add(string path,string node,string element,string value,int i){  XmlDocument doc = new XmlDocument();  doc.Load(path);  XmlNodeList nodeList = doc.SelectNodes(node);  XmlNode newNode = doc.SelectSingleNode(node).LastChild;  if (i < 0 || i > nodeList.Count-1)//如果小於0或者大於節點長度,預設從最後一個節點添加  { XmlElement newElement = doc.CreateElement(element);//建立元素 newElement.InnerText = value;//賦值 newNode.AppendChild(newElement);  }  else  { XmlElement newElement = doc.CreateElement(element); newElement.InnerText = value; nodeList[i - 1].AppendChild(newElement);  }  doc.Save(path);  return true;}

希望本文所述方法對大家的C#程式設計能有所協助。

除聲明外, 跑步客文章均為原創,轉載請以連結形式標明本文地址
  C#操作XML檔案執行個體匯總

本文地址:  http://www.paobuke.com/develop/c-develop/pbk23627.html






相關內容C#調用python檔案執行asp.net core mvc許可權控制:在視圖中控制操作許可權C#驗證碼的建立與使用樣本C#利用時間和隨即字串建立唯一的訂單編號
深入解析C#中的交錯數組與隱式類型的數組C#如何開啟並讀取usb的檔案目錄C#中簡單的裝箱操作執行個體分析使用C#擷取網頁HTML源碼的例子

C#操作XML檔案執行個體匯總

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.