


Detailed explanation of code examples of C#-XML operation class
Write an XML operation class, 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> public class XmlHelper { public XmlHelper() { } /// <summary> /// 读取数据 /// </summary> /// <param name="path">路径</param> /// <param name="node">节点</param> /// <param name="attribute">属性名,非空时返回该属性值,否则返回串联值</param> /// <returns>string</returns> /************************************************** * 使用示列: * XmlHelper.Read(path, "/Node", "") * XmlHelper.Read(path, "/Node/Element[@Attribute='Name']", "Attribute") ************************************************/ 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> /// 插入数据 /// </summary> /// <param name="path">路径</param> /// <param name="node">节点</param> /// <param name="element">元素名,非空时插入新元素,否则在该元素中插入属性</param> /// <param name="attribute">属性名,非空时插入该元素属性值,否则插入元素值</param> /// <param name="value">值</param> /// <returns></returns> /************************************************** * 使用示列: * XmlHelper.Insert(path, "/Node", "Element", "", "Value") * XmlHelper.Insert(path, "/Node", "Element", "Attribute", "Value") * XmlHelper.Insert(path, "/Node", "", "Attribute", "Value") ************************************************/ 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(element); if (attribute.Equals("")) xe.InnerText = value; else xe.SetAttribute(attribute, value); xn.AppendChild(xe); } doc.Save(path); } catch { } } /// <summary> /// 修改数据 /// </summary> /// <param name="path">路径</param> /// <param name="node">节点</param> /// <param name="attribute">属性名,非空时修改该节点属性值,否则修改节点值</param> /// <param name="value">值</param> /// <returns></returns> /************************************************** * 使用示列: * XmlHelper.Insert(path, "/Node", "", "Value") * XmlHelper.Insert(path, "/Node", "Attribute", "Value") ************************************************/ 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> /// 删除数据 /// </summary> /// <param name="path">路径</param> /// <param name="node">节点</param> /// <param name="attribute">属性名,非空时删除该节点属性值,否则删除节点值</param> /// <param name="value">值</param> /// <returns></returns> /************************************************** * 使用示列: * XmlHelper.Delete(path, "/Node", "") * XmlHelper.Delete(path, "/Node", "Attribute") ************************************************/ 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"); //插入元素 //XmlHelper.Insert(xml, "/Root", "Studio", "", ""); //插入元素/属性 //XmlHelper.Insert(xml, "/Root/Studio", "Site", "Name", "小路工作室"); //XmlHelper.Insert(xml, "/Root/Studio", "Site", "Name", "丁香鱼工作室"); //XmlHelper.Insert(xml, "/Root/Studio", "Site", "Name", "五月软件"); //XmlHelper.Insert(xml, "/Root/Studio/Site[@Name='五月软件]", "Master", "", "五月"); //插入属性 //XmlHelper.Insert(xml, "/Root/Studio/Site[@Name='小路工作室']", "", "Url", "http://www.wzlu.com/"); //XmlHelper.Insert(xml, "/Root/Studio/Site[@Name='丁香鱼工作室']", "", "Url", "http://www.luckfish.net/"); //XmlHelper.Insert(xml, "/Root/Studio/Site[@Name='五月软件]", "", "Url", "http://www.vs2005.com.cn/"); //修改元素值 //XmlHelper.Update(xml, "/Root/Studio/Site[@Name='五月软件]/Master", "", "Wuyue"); //修改属性值 //XmlHelper.Update(xml, "/Root/Studio/Site[@Name='五月软件]", "Url", "http://www.vs2005.com.cn/"); //XmlHelper.Update(xml, "/Root/Studio/Site[@Name='五月软件]", "Name", "MaySoft"); //读取元素值 //Response.Write("<p>" + XmlHelper.Read(xml, "/Root/Studio/Site/Master", "") + "</p>"); //读取属性值 //Response.Write("<p>" + XmlHelper.Read(xml, "/Root/Studio/Site", "Url") + "</p>"); //读取特定属性值 //Response.Write("<p>" + XmlHelper.Read(xml, "/Root/Studio/Site[@Name='丁香鱼工作室']", "Url") + "</p>"); //删除属性 //XmlHelper.Delete(xml, "/Root/Studio/Site[@Name='小路工作室']", "Url"); //删除元素 //XmlHelper.Delete(xml, "/Root/Studio", "");
The above is the C#-XML operation class For detailed code examples, please pay attention to the PHP Chinese website (www.php.cn) for more related content!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Guide to Active Directory with C#. Here we discuss the introduction and how Active Directory works in C# along with the syntax and example.

Guide to Random Number Generator in C#. Here we discuss how Random Number Generator work, concept of pseudo-random and secure numbers.

Guide to Factorial in C#. Here we discuss the introduction to factorial in c# along with different examples and code implementation.

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Guide to Patterns in C#. Here we discuss the introduction and top 3 types of Patterns in C# along with its examples and code implementation.

The difference between multithreading and asynchronous is that multithreading executes multiple threads at the same time, while asynchronously performs operations without blocking the current thread. Multithreading is used for compute-intensive tasks, while asynchronously is used for user interaction. The advantage of multi-threading is to improve computing performance, while the advantage of asynchronous is to not block UI threads. Choosing multithreading or asynchronous depends on the nature of the task: Computation-intensive tasks use multithreading, tasks that interact with external resources and need to keep UI responsiveness use asynchronous.

Guide to Prime Numbers in C#. Here we discuss the introduction and examples of prime numbers in c# along with code implementation.

There are three ways to convert XML to Word: use Microsoft Word, use an XML converter, or use a programming language.
