C#操作XML文件實例匯總。本站提示廣大學習愛好者:(C#操作XML文件實例匯總)文章只能為提供參考,不一定能成為您想要的結果。以下是C#操作XML文件實例匯總正文
針對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#法式設計能有所贊助。