zoukankan      html  css  js  c++  java
  • XmlHelper 工具类

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Xml;

    namespace EasyFrame.Common
    {
    public class XmlHelper
    {
    #region 增、删、改操作==============================================

    /// <summary>
    /// 追加节点
    /// </summary>
    /// <param name="filePath">XML文档绝对路径</param>
    /// <param name="xPath">范例: @"Skill/First/SkillItem"</param>
    /// <param name="xmlNode">XmlNode节点</param>
    /// <returns></returns>
    public static bool AppendChild(string filePath, string xPath, XmlNode xmlNode)
    {
    try
    {
    XmlDocument doc = new XmlDocument();
    doc.Load(filePath);
    XmlNode xn = doc.SelectSingleNode(xPath);
    XmlNode n = doc.ImportNode(xmlNode, true);
    xn.AppendChild(n);
    doc.Save(filePath);
    return true;
    }
    catch
    {
    return false;
    }
    }

    /// <summary>
    /// 从XML文档中读取节点追加到另一个XML文档中
    /// </summary>
    /// <param name="filePath">需要读取的XML文档绝对路径</param>
    /// <param name="xPath">范例: @"Skill/First/SkillItem"</param>
    /// <param name="toFilePath">被追加节点的XML文档绝对路径</param>
    /// <param name="toXPath">范例: @"Skill/First/SkillItem"</param>
    /// <returns></returns>
    public static bool AppendChild(string filePath, string xPath, string toFilePath, string toXPath)
    {
    try
    {
    XmlDocument doc = new XmlDocument();
    doc.Load(toFilePath);
    XmlNode xn = doc.SelectSingleNode(toXPath);

    XmlNodeList xnList = ReadNodes(filePath, xPath);
    if (xnList != null)
    {
    foreach (XmlElement xe in xnList)
    {
    XmlNode n = doc.ImportNode(xe, true);
    xn.AppendChild(n);
    }
    doc.Save(toFilePath);
    }
    return true;
    }
    catch
    {
    return false;
    }
    }

    /// <summary>
    /// 修改节点的InnerText的值
    /// </summary>
    /// <param name="filePath">XML文件绝对路径</param>
    /// <param name="xPath">范例: @"Skill/First/SkillItem"</param>
    /// <param name="value">节点的值</param>
    /// <returns></returns>
    public static bool UpdateNodeInnerText(string filePath, string xPath, string value)
    {
    try
    {
    XmlDocument doc = new XmlDocument();
    doc.Load(filePath);
    XmlNode xn = doc.SelectSingleNode(xPath);
    XmlElement xe = (XmlElement)xn;
    xe.InnerText = value;
    doc.Save(filePath);
    }
    catch
    {
    return false;
    }
    return true;
    }

    /// <summary>
    /// 读取XML文档
    /// </summary>
    /// <param name="filePath">XML文件绝对路径</param>
    /// <returns></returns>
    public static XmlDocument LoadXmlDoc(string filePath)
    {
    try
    {
    XmlDocument doc = new XmlDocument();
    doc.Load(filePath);
    return doc;
    }
    catch
    {
    return null;
    }
    }
    #endregion 增、删、改操作

    #region 扩展方法===================================================
    /// <summary>
    /// 读取XML的所有子节点
    /// </summary>
    /// <param name="filePath">XML文件绝对路径</param>
    /// <param name="xPath">范例: @"Skill/First/SkillItem"</param>
    /// <returns></returns>
    public static XmlNodeList ReadNodes(string filePath, string xPath)
    {
    try
    {
    XmlDocument doc = new XmlDocument();
    doc.Load(filePath);
    XmlNode xn = doc.SelectSingleNode(xPath);
    XmlNodeList xnList = xn.ChildNodes; //得到该节点的子节点
    return xnList;
    }
    catch
    {
    return null;
    }
    }

    #endregion 扩展方法
    }
    }

  • 相关阅读:
    Ajax调用WCF报405错误
    字符串转json方法
    正则取括号里面的内容
    string[] 转int[] 的方法
    C# 中结构与类的区别
    通用的权限模块是如何设计的?
    .net打包自动安装数据库!
    VS.net 2005快捷键一览表
    Windows Forms DataGridView 中合并单元格
    VS2005 制作安装程序
  • 原文地址:https://www.cnblogs.com/opts/p/7406453.html
Copyright © 2011-2022 走看看