Operations against XML files are a very common feature in C# programming. This article shows several common examples of C# operating XML files using examples. The details are as follows:
1. Return to the node subscript
public static XmlDocument getDoc(String path)//Load the xml document{ XmlDocument doc = new XmlDocument(); (path); return doc; } /// <summary> /// Return to the found node subscript/// </summary> /// <param name="path">xml file path</param>/// <param name="bname">Book title</param>/// <returns></returns> public static int getPosition(String path,string node, String bname) { int i; XmlDocument doc = new XmlDocument(); (path); XmlNodeList nodeList = (node).ChildNodes; for (i = 0; i < ; i++) { if (nodeList[i].ChildNodes[0].InnerText == bname) { return i; } } return -1; }
2. Traverse the data
/// <summary> /// traversal of data/// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnRead_Click(object sender, EventArgs e) { XmlDocument doc = getDoc("");//Load the xml document, the books file is stored in foreach (XmlElement root in )//Transfer the root child node { ("<script>('<br/>');</script>"); ("<script>('" + + "');</script>");//Node name book foreach (XmlElement item in ) { ("<script>('" + + "'+':'+'" + + "');</script>");//Output node name and text node value ("<script>('<br/>');</script>"); } } }
3. Search
/// <summary> /// Find/// </summary> /// <param name="path">File path</param>/// <param name="node">node</param>/// <param name="bname">Find keywords</param>/// <returns>XmlNode</returns> public static XmlNode Find(string path,string node,string bname) { XmlDocument doc = new XmlDocument(); (path);//Load the xml document XmlNodeList nodeList = (node).ChildNodes; int i = getPosition(path, node, bname);// if (i >= 0) return nodeList[i]; else return null; }
4. Delete nodes
/// <summary> /// Delete elements and attributes/// </summary> /// <param name="path">file</param>/// <param name="node">Specify the parent node of the node</param>/// <param name="attribute">Delete the node when it is empty, otherwise delete the attribute</param>/// <param name="bname"></param> public static void Delete(string path,string node,string attribute,string bname) { XmlDocument doc = new XmlDocument(); (path); XmlNode root = (node); XmlNodeList nodeList = (node).ChildNodes; int i = getPosition(path, node, bname);//Return the specified node subscript if (i >= 0) { if (("")) { (nodeList[i]); } else { XmlElement xn = (XmlElement)nodeList[i]; (attribute); } } (path); }
5. Add
/// <summary> /// Add element value/// </summary> /// <param name="path"></param> /// <param name="node"></param> /// <param name="element"></param> /// <param name="value"></param> /// <param name="i">Inserted subscript, if negative, insert from the last node by default</param>/// <returns></returns> public static bool Add(string path,string node,string element,string value,int i) { XmlDocument doc = new XmlDocument(); (path); XmlNodeList nodeList = (node); XmlNode newNode = (node).LastChild; if (i < 0 || i > -1)//If it is less than 0 or greater than the node length, it will be added from the last node by default { XmlElement newElement = (element);//Create an element = value;//Assignment (newElement); } else { XmlElement newElement = (element); = value; nodeList[i - 1].AppendChild(newElement); } (path); return true; }
I hope that the methods described in this article will be helpful to everyone's C# programming.