欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

基于C#实现XML文件读取工具类

程序员文章站 2023-11-20 22:19:10
本文实例讲述了基于c#实现xml文件读取工具类。分享给大家供大家参考。具体如下: 这是我去年写的一个xml文件读取工具类,现在做了一些调整 基本能满足一般的xml文件...

本文实例讲述了基于c#实现xml文件读取工具类。分享给大家供大家参考。具体如下:

这是我去年写的一个xml文件读取工具类,现在做了一些调整
基本能满足一般的xml文件读取需求。
更新:
2013年6月26日19:37 fix了几个bug,为所有方法添加单元测试,并运行通过;同时调整了几个方法的位置。

/// <summary>
/// author: jiangxiaoqiang
/// </summary>
public class xmlreader
{
  //========================================================= //
  #region 获取xmldocument对象
  /// <summary>
  /// 根据xml文件内容获取xmldocument对象
  /// </summary>
  /// <param name="xmlfilecontent"></param>
  /// <returns></returns>
  public static xmldocument getxmldocbyxmlcontent(string xmlfilecontent)
  {
    if (string.isnullorempty(xmlfilecontent))
    {
      return null;
    }
    var xdoc = new xmldocument();
    try
    {
      xdoc.loadxml(xmlfilecontent);
    }
    catch
    {
      xdoc = null;
    }
    return xdoc;
  }
  /// <summary>
  /// 根据xml文件路径获取xmldocument对象
  /// </summary>
  /// <param name="xmlfilepath"></param>
  /// <returns></returns>
  public static xmldocument getxmldocbyfilepath(string xmlfilepath)
  {
    if (string.isnullorempty(xmlfilepath) || !file.exists(xmlfilepath))
    {
      return null;
    }
    var xdoc = new xmldocument();
    try
    {
      xdoc.load(xmlfilepath);
    }
    catch
    {
      throw new exception(string.format("请确认该xml文件格式正确,路径为:{0}", xmlfilepath));
    }
    return xdoc;
  }
  #endregion
  //========================================================= //
  //========================================================= //
  #region 获取xml节点(或节点列表)
  /// <summary>
  /// 获取父节点下指定节点名称的第一个子节点
  /// </summary>
  /// <param name="parentxmlnode"></param>
  /// <param name="childnodename"></param>
  /// <returns></returns>
  public static xmlnode getfirstchildnodebyname(xmlnode parentxmlnode, string childnodename)
  {
    var childxmlnodes = getchildnodesbyname(parentxmlnode, childnodename);
    if (childxmlnodes != null && childxmlnodes.count > 0)
    {
      return childxmlnodes[0];
    }
    return null;
  }
  /// <summary>
  /// 获取父节点下指定节点名称的子节点列表
  /// </summary>
  /// <param name="parentxmlnode">父节点</param>
  /// <param name="nodename">节点名称</param>
  /// <returns></returns>
  public static xmlnodelist getchildnodesbyname(xmlnode parentxmlnode, string nodename)
  {
    if (parentxmlnode == null || string.isnullorempty(nodename))
    {
      return null;
    }
    return getchildnodesbyxpathexpr(parentxmlnode, string.format(".//{0}", nodename));
  }
  /// <summary>
  /// 获取父节点下满足xpathexpr表达式的xml子节点列表
  /// </summary>
  /// <param name="parentxmlnode">父节点</param>
  /// <param name="xpathexpr"></param>
  /// <returns></returns>  
  public static xmlnodelist getchildnodesbyxpathexpr(xmlnode parentxmlnode, string xpathexpr)
  {
    if (parentxmlnode == null || string.isnullorempty(xpathexpr))
    {
      return null;
    }
    return parentxmlnode.selectnodes(xpathexpr);
  }
  /// <summary>
  /// 获取父节点下的第一个子节点
  /// </summary>
  /// <param name="parentxmlnode"></param>
  /// <returns></returns>
  public static xmlnode getfirstchildnode(xmlnode parentxmlnode)
  {
    var childxmlnodes = getchildnodes(parentxmlnode);
    if (childxmlnodes != null && childxmlnodes.count > 0)
    {
      return childxmlnodes[0];
    }
    return null;
  }
  /// <summary>
  /// 获取父节点的子节点列表
  /// </summary>
  /// <param name="parentxmlnode">父节点</param>
  /// <returns></returns>
  public static xmlnodelist getchildnodes(xmlnode parentxmlnode)
  {
    return parentxmlnode == null ? null : parentxmlnode.childnodes;
  }
  #endregion
  //========================================================= //
  //========================================================= //
  #region 读取节点属性值
  /// <summary>
  /// 读取某个xml节点的属性值(根据属性名)
  /// </summary>
  /// <param name="xmlnode"></param>
  /// <param name="attrname"></param>
  /// <returns></returns>
  public static string readattrvalue(xmlnode xmlnode, string attrname)
  {
    var xmlelement = xmlnode as xmlelement;
    return xmlelement == null ? null : xmlelement.getattribute(attrname);
  }
  /// <summary>
  /// 读取父节点下指定节点名和属性名的第一个子节点的属性值
  /// </summary>
  /// <param name="parentxmlnode">xml父节点</param>
  /// <param name="childnodename">节点名称</param>
  /// <param name="attrname">属性名</param>
  /// <returns></returns>
  public static string readfirstattrvalue(xmlnode parentxmlnode, string childnodename, string attrname)
  {
    var attrvals = readattrvalues(parentxmlnode, childnodename, attrname);
    return (attrvals == null || attrvals.length == 0) ? null : attrvals[0];
  }
  /// <summary>
  /// 读取父节点下指定节点名和属性名的所有子节点的该属性值的数组
  /// </summary>
  /// <param name="parentxmlnode">xml文档</param>
  /// <param name="childnodename">节点名称</param>
  /// <param name="attrname">属性名</param>
  /// <returns></returns>
  public static string[] readattrvalues(xmlnode parentxmlnode, string childnodename, string attrname)
  {
    if (parentxmlnode == null || string.isnullorempty(childnodename) || string.isnullorempty(attrname))
    {
      return null;
    }
    var xpathexpr = string.format("//{0}[@{1}]", childnodename, attrname);
    var nodes = getchildnodesbyxpathexpr(parentxmlnode, xpathexpr);
    if (nodes != null && nodes.count > 0)
    {
      var nodecount = nodes.count;
      var attrvals = new string[nodecount];
      for (var i = 0; i < nodecount; i++)
      {
        attrvals[i] = ((xmlelement)nodes[i]).getattribute(attrname);
      }
      return attrvals;
    }
    return null;
  }
  #endregion
  //========================================================= //
  //========================================================= //
  #region 读取父节点下的子节点的文本内容
  /// <summary>
  /// 读取父节点下指定节点名的第一个子节点的文本
  /// </summary>
  /// <param name="parentxmlnode"></param>
  /// <param name="childnodename"></param>
  /// <returns></returns>
  public static string readfirstchildnodetextbyname(xmlnode parentxmlnode, string childnodename)
  {
    var childnodetexts = readchildnodetextsbyname(parentxmlnode, childnodename);
    if (childnodetexts != null && childnodetexts.length > 0)
    {
      return childnodetexts[0];
    }
    return null;
  }
  /// <summary>
  /// 读取父节点下指定节点名的所有子节点的文本数组
  /// </summary>
  /// <param name="parentxmlnode"></param>
  /// <param name="childnodename"></param>
  /// <returns></returns>
  public static string[] readchildnodetextsbyname(xmlnode parentxmlnode, string childnodename)
  {
    if (parentxmlnode == null || string.isnullorempty(childnodename))
    {
      return null;
    }
    var xpathexpr = string.format(".//{0}", childnodename);
    var childnodes = getchildnodesbyxpathexpr(parentxmlnode, xpathexpr);
    if (childnodes != null && childnodes.count > 0)
    {
      var nodecount = childnodes.count;
      var nodetexts = new string[nodecount];
      for (var i = 0; i < nodecount; i++)
      {
        nodetexts[i] = childnodes[i].innertext;
      }
      return nodetexts;
    }
    return null;
  }
  /// <summary>
  /// 读取父节点下的第一个子节点的文本
  /// </summary>
  /// <param name="parentxmlnode"></param>
  /// <returns></returns>
  public static string readfirstchildnodetext(xmlnode parentxmlnode)
  {
    var childnodetexts = readchildnodetexts(parentxmlnode);
    if (childnodetexts != null && childnodetexts.length > 0)
    {
      return childnodetexts[0];
    }
    return null;
  }
  /// <summary>
  /// 读取父节点下的所有子节点的文本数组
  /// </summary>
  /// <param name="parentxmlnode"></param>
  /// <returns></returns>
  public static string[] readchildnodetexts(xmlnode parentxmlnode)
  {
    if (parentxmlnode == null)
    {
      return null;
    }
    var childnodes = getchildnodes(parentxmlnode);
    if (childnodes != null && childnodes.count > 0)
    {
      var nodecount = childnodes.count;
      var nodetexts = new string[nodecount];
      for (var i = 0; i < nodecount; i++)
      {
        nodetexts[i] = childnodes[i].innertext;
      }
      return nodetexts;
    }
    return null;
  }
  /// <summary>
  /// 读取xml节点文本
  /// </summary>
  /// <param name="xmlnode"></param>
  /// <returns></returns>
  public static string readnodetext(xmlnode xmlnode)
  {
    if (xmlnode == null)
    {
      return null;
    }
    return xmlnode.innertext;
  }
  #endregion
  //========================================================= //
}

希望本文所述对大家的c#程序设计有所帮助。