SoFunction
Updated on 2025-03-06

Summary of C# operation XML file instances

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>/// &lt;returns&gt;&lt;/returns&gt;
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 &lt; ; i++)
  {
 if (nodeList[i].ChildNodes[0].InnerText == bname)
 {
   return i;
 }
  }
  return -1;
}

2. Traverse the data

/// &lt;summary&gt;
/// traversal of data/// &lt;/summary&gt;
/// &lt;param name="sender"&gt;&lt;/param&gt;
/// &lt;param name="e"&gt;&lt;/param&gt;
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  {
 ("&lt;script&gt;('&lt;br/&gt;');&lt;/script&gt;");
 ("&lt;script&gt;('" +  + "');&lt;/script&gt;");//Node name book foreach (XmlElement item in )
 {
   ("&lt;script&gt;('" +  + "'+':'+'" +  + "');&lt;/script&gt;");//Output node name and text node value   ("&lt;script&gt;('&lt;br/&gt;');&lt;/script&gt;");
 }
  }
}

3. Search

/// &lt;summary&gt;
/// Find/// &lt;/summary&gt;
/// <param name="path">File path</param>/// <param name="node">node</param>/// <param name="bname">Find keywords</param>/// &lt;returns&gt;XmlNode&lt;/returns&gt;
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 &gt;= 0)
 return nodeList[i];
  else
 return null;
}

4. Delete nodes

/// &lt;summary&gt;
/// Delete elements and attributes/// &lt;/summary&gt;
/// <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>/// &lt;param name="bname"&gt;&lt;/param&gt;
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 &gt;= 0)
  {
 if ((""))
 {
   (nodeList[i]);
 }
 else
 {
   XmlElement xn = (XmlElement)nodeList[i];
   (attribute);
 }
  }
  (path);
}

5. Add

/// &lt;summary&gt;
/// Add element value/// &lt;/summary&gt;
/// &lt;param name="path"&gt;&lt;/param&gt;
/// &lt;param name="node"&gt;&lt;/param&gt;
/// &lt;param name="element"&gt;&lt;/param&gt;
/// &lt;param name="value"&gt;&lt;/param&gt;
/// <param name="i">Inserted subscript, if negative, insert from the last node by default</param>/// &lt;returns&gt;&lt;/returns&gt;
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 &lt; 0 || i &gt; -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.