An instance of XML operation class implemented by C
- 2021-07-24 11:40:13
- OfStack
This article illustrates the XML operation class implemented by C #. Share it for your reference. The details are as follows:
Here is one XML operation class written by C #, including read/insert/modify/delete.
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
namespace PuTianCheng
{
/// <summary>
/// XmlHelper Summary description of
/// </summary>
public class XmlHelper
{
public XmlHelper()
{
}
/// <summary>
/// Read data
/// </summary>
/// <param name="path"> Path </param>
/// <param name="node"> Node </param>
/// <param name="attribute"> Attribute name, which returns the attribute value if it is not empty, otherwise it returns the concatenated value </param>
/// <returns>string</returns>
public static string Read(string path, string node, string attribute)
{
string value = "";
try
{
XmlDocument doc = new XmlDocument();
doc.Load(path);
XmlNode xn = doc.SelectSingleNode(node);
value = (attribute.Equals("") ? xn.InnerText : xn.Attributes[attribute].Value);
}
catch { }
return value;
}
/// <summary>
/// Insert data
/// </summary>
/// <param name="path"> Path </param>
/// <param name="node"> Node </param>
/// <param name="element"> Element name, insert a new element if it is not empty, otherwise insert an attribute in the element </param>
/// <param name="attribute"> Attribute name, insert the element attribute value if it is not empty, otherwise insert the element value </param>
/// <param name="value"> Value </param>
/// <returns></returns>
public static void Insert(string path, string node, string element, string attribute, string value)
{
try
{
XmlDocument doc = new XmlDocument();
doc.Load(path);
XmlNode xn = doc.SelectSingleNode(node);
if (element.Equals(""))
{
if (!attribute.Equals(""))
{
XmlElement xe = (XmlElement)xn;
xe.SetAttribute(attribute, value);
}
}
else
{
XmlElement xe = doc.createElement_x(element);
if (attribute.Equals(""))
xe.InnerText = value;
else
xe.SetAttribute(attribute, value);
xn.AppendChild(xe);
}
doc.Save(path);
}
catch { }
}
/// <summary>
/// Modify data
/// </summary>
/// <param name="path"> Path </param>
/// <param name="node"> Node </param>
/// <param name="attribute"> Attribute name. If it is not empty, modify the attribute value of this node; Otherwise, modify the node value </param>
/// <param name="value"> Value </param>
/// <returns></returns>
public static void Update(string path, string node, string attribute, string value)
{
try
{
XmlDocument doc = new XmlDocument();
doc.Load(path);
XmlNode xn = doc.SelectSingleNode(node);
XmlElement xe = (XmlElement)xn;
if (attribute.Equals(""))
xe.InnerText = value;
else
xe.SetAttribute(attribute, value);
doc.Save(path);
}
catch { }
}
/// <summary>
/// Delete data
/// </summary>
/// <param name="path"> Path </param>
/// <param name="node"> Node </param>
/// <param name="attribute"> Attribute name. If it is not empty, delete the node attribute value; Otherwise, delete the node value </param>
/// <param name="value"> Value </param>
/// <returns></returns>
public static void Delete(string path, string node, string attribute)
{
try
{
XmlDocument doc = new XmlDocument();
doc.Load(path);
XmlNode xn = doc.SelectSingleNode(node);
XmlElement xe = (XmlElement)xn;
if (attribute.Equals(""))
xn.ParentNode.RemoveChild(xn);
else
xe.RemoveAttribute(attribute);
doc.Save(path);
}
catch { }
}
}
}
XmlFile. xml:
<?xml version="1.0" encoding="utf-8"?>
<Root />
Usage:
string xml = Server.MapPath("XmlFile.xml");
// Insert element
XmlHelper.Insert(xml, "/Root", "Studio", "", "");
// Insert element / Attribute
XmlHelper.Insert(xml, "/Root/Studio", "Site", "Name", " Alley Studio ");
XmlHelper.Insert(xml, "/Root/Studio", "Site", "Name", " Clove Fish Studio ");
XmlHelper.Insert(xml, "/Root/Studio", "Site", "Name", " Putiancheng studio ");
XmlHelper.Insert(xml, "/Root/Studio/Site[@Name=' Putiancheng studio ']", "Master", "", " Meditation on the world of mortals ");
// Insert attribute
XmlHelper.Insert(xml, "/Root/Studio/Site[@Name=' Alley Studio ']", "", "Url", "http://www.wzlu.com/");
XmlHelper.Insert(xml, "/Root/Studio/Site[@Name=' Clove Fish Studio ']", "", "Url", "http://www.luckfish.net/");
XmlHelper.Insert(xml, "/Root/Studio/Site[@Name=' Putiancheng studio ']", "", "Url", "http://www.putiancheng.com/");
// Modify element value
XmlHelper.Update(xml, "/Root/Studio/Site[@Name=' Putiancheng studio ']/Master", "", "RedDust");
// Modify property values
XmlHelper.Update(xml, "/Root/Studio/Site[@Name=' Putiancheng studio ']", "Url", "http://www.putiancheng.net/");
XmlHelper.Update(xml, "/Root/Studio/Site[@Name=' Putiancheng studio ']", "Name", "PuTianCheng Studio");
// Read element value
Response.Write("<div>" + XmlHelper.Read(xml, "/Root/Studio/Site/Master", "") + "</div>");
// Read property values
Response.Write("<div>" + XmlHelper.Read(xml, "/Root/Studio/Site", "Url") + "</div>");
// Read the value of a specific attribute
Response.Write("<div>" + XmlHelper.Read(xml, "/Root/Studio/Site[@Name=' Clove Fish Studio ']", "Url") + "</div>");
// Delete attribute
XmlHelper.Delete(xml, "/Root/Studio/Site[@Name=' Alley Studio ']", "Url");
// Delete Element
XmlHelper.Delete(xml, "/Root/Studio", "");
I hope this article is helpful to everyone's C # programming.