打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
分享XmlHelper类
using System.Xml;using System;/// <summary>///XmlHelper类,用于处理Xml文档的相关操作/// </summary>public class XmlHelper{    /// <summary>    /// 读取Xml文档    /// 使用示列:    /// XmlHelper.XmlDocument(path);    /// </summary>    /// <param name="path">文件路径</param>    /// <param name="node">节点名称</param>    /// <param name="attribute">特性名称</param>    /// <returns>string</returns>    public static XmlDocument ReadXmlNode(string path)    {        try        {            XmlDocument doc = new XmlDocument();            doc.Load(path);            return doc;        }        catch        {            return null;        }    }    /// <summary>    /// 读取Xml文档节点数据    /// 使用示列:    /// XmlHelper.ReadXmlNode(path, "nodeName");    /// </summary>    /// <param name="path">文件路径</param>    /// <param name="node">节点名称</param>    /// <param name="attribute">特性名称</param>    /// <returns>string</returns>    public static XmlNode ReadXmlNode(string path, string nodeName)    {        XmlNode value = null;        try        {            XmlDocument doc = new XmlDocument();            doc.Load(path);            value = doc.SelectSingleNode(nodeName);        }        catch        { }        return value;    }    /// <summary>    /// 读取Xml文档节点数据    /// 使用示列:    /// XmlHelper.ReadXmlNodeText(path, "node", "");    /// XmlHelper.ReadXmlNodeText(path, "node", "attribute");    /// </summary>    /// <param name="path">文件路径</param>    /// <param name="node">节点名称</param>    /// <param name="attribute">特性名称</param>    /// <returns>string</returns>    public static string ReadXmlNodeText(string path, string node, string attribute)    {        string value = "";        try        {            XmlDocument doc = new XmlDocument();            doc.Load(path);            XmlNode no = doc.SelectSingleNode(node);            value = (string.IsNullOrEmpty(attribute)) ? no.InnerText : no.Attributes[attribute].Value;        }        catch        { }        return value;    }    /// <summary>    /// 设置XmlNode属性    /// 使用示列:    /// XmlHelper.SetAttribute(path, nodeName, "");    /// XmlHelper.SetAttribute(path, nodeName, attributeName,attributeValue);    /// </summary>    /// <param name="path">文件路径</param>    /// <param name="nodeName">节点名称</param>    /// <param name="attributeName">属性名称</param>    /// <param name="attributeValue">属性值</param>    /// <returns>void</returns>    public static void SetAttribute(string path, string nodeName, string attributeName, string attributeValue)    {        try        {            XmlDocument doc = new XmlDocument();            doc.Load(path);            XmlElement element = doc.SelectSingleNode(nodeName) as XmlElement;            if (element == null)            {                throw new Exception("节点元素不存在!");            }            else            {                if (attributeName.Equals(""))                {                    element.InnerText = attributeValue;                }                else                {                    element.SetAttribute(attributeName, attributeValue);                }                doc.Save(path);            }        }        catch        { }    }    /// <summary>    /// 删除XmlNode属性    /// 使用示列:    /// XmlHelper.DeleteAttribute(path, nodeName, "");    /// XmlHelper.DeleteAttribute(path, nodeName, attributeName,attributeValue);    /// </summary>    /// <param name="path">文件路径</param>    /// <param name="nodeName">节点名称</param>    /// <param name="attributeName">属性名称</param>    /// <returns>void</returns>    public static void DeleteAttribute(string path, string nodeName, string attributeName)    {        if (attributeName.Equals(""))        {            return;        }        try        {            XmlDocument doc = new XmlDocument();            doc.Load(path);            XmlElement element = doc.SelectSingleNode(nodeName) as XmlElement;            if (element == null)            {                throw new Exception("节点元素不存在!");            }            else            {                element.RemoveAttribute(attributeName);                doc.Save(path);            }        }        catch        { }    }    /// <summary>    /// 读取XmlNode的所以子节点    /// 使用示列:    /// XmlHelper.ReadNodeList(path, nodeName);    /// </summary>    /// <param name="path">文件路径</param>    /// <param name="nodeName">节点名称</param>    /// <returns>XmlNodeList</returns>    public static XmlNodeList ReadNodeList(string path, string nodeName)    {        try        {            XmlDocument doc = new XmlDocument();            doc.Load(path);            XmlNode element = doc.SelectSingleNode(nodeName);            if (element == null)            {                throw new Exception("节点元素不存在!");            }            else            {                return element.ChildNodes;            }        }        catch        {            return null;        }    }}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
一个经过改良的XMLHelper(包含了序列化,反序列化,创建xml文件,读取节点,C#对...
C#:XML操作类
C#操作Xml通用工具类-程序开发-红黑联盟
delphi读写XML
C#XmlHelper操作Xml文档的帮助类
C# 操作xml文件之Linq To Xml
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服