C operates the XML file instance summary
- 2020-08-22 22:38:18
- OfStack
Working with XML files is a very common feature in C# programming. This article demonstrates a few common examples of C# working with XML files. The details are as follows:
1. Return node index
public static XmlDocument getDoc(String path)// loading xml The document
{
XmlDocument doc = new XmlDocument();
doc.Load(path);
return doc;
}
/// <summary>
/// Returns the index of the node found
/// </summary>
/// <param name="path">xml The file path </param>
/// <param name="bname"> Title: </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. Traverse the data
/// <summary>
/// Through the data
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnRead_Click(object sender, EventArgs e)
{
XmlDocument doc = getDoc("books.xml");// loading xml The document, books File in
foreach (XmlElement root in doc.DocumentElement.ChildNodes)// Iterate over the child nodes of the root
{
Response.Write("<script>document.write('<br/>');</script>");
Response.Write("<script>document.write('" + root.Name + "');</script>");// The node name root.Name book
foreach (XmlElement item in root.ChildNodes)
{
Response.Write("<script>document.write('" + item.Name + "'+':'+'" + item.InnerText + "');</script>");// Outputs the node name and the value of the text node
Response.Write("<script>document.write('<br/>');</script>");
}
}
}
3. Look for
/// <summary>
/// To find the
/// </summary>
/// <param name="path"> The 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();
doc.Load(path);// loading xml The document
XmlNodeList nodeList = doc.SelectSingleNode(node).ChildNodes;
int i = getPosition(path, node, bname);//
if (i >= 0)
return nodeList[i];
else
return null;
}
4. Delete nodes
/// <summary>
/// Deletes elements, attributes
/// </summary>
/// <param name="path"> file </param>
/// <param name="node"> Specifies the parent node of the node </param>
/// <param name="attribute"> Deletes the node when null, or deletes the property </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);// Returns the specified node index
if (i >= 0)
{
if (attribute.Equals(""))
{
root.RemoveChild(nodeList[i]);
}
else
{
XmlElement xn = (XmlElement)nodeList[i];
xn.RemoveAttribute(attribute);
}
}
doc.Save(path);
}
5. Add
/// <summary>
/// Add element values
/// </summary>
/// <param name="path"></param>
/// <param name="node"></param>
/// <param name="element"></param>
/// <param name="value"></param>
/// <param name="i"> Insert subscript, if negative, default from last 1 Node insertion </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)// If less than 0 Or greater than the node length, default from last 1 Node addition
{
XmlElement newElement = doc.CreateElement(element);// Create the element
newElement.InnerText = value;// The assignment
newNode.AppendChild(newElement);
}
else
{
XmlElement newElement = doc.CreateElement(element);
newElement.InnerText = value;
nodeList[i - 1].AppendChild(newElement);
}
doc.Save(path);
return true;
}
Hopefully, the approach described in this article has helped you with your C# programming.