现在的位置: 首页 > 综合 > 正文

.net xml常用操作方法

2013年10月08日 ⁄ 综合 ⁄ 共 12143字 ⁄ 字号 评论关闭

using System;
using System.Xml;
using System.Xml.Xsl;
using System.Xml.XPath;
using System.Web;

namespace Cloth.Common
{
 /// <summary>
 /// XmlHelper 的摘要说明。
 /// </summary>
 public class XmlHelper
 {
  public enum EnumXmlPathType
  {
   AbsolutePath,
   VirtualPath
  }

  private string xmlfilepath ;
  private EnumXmlPathType xmlfilepathtype ;
  private XmlDocument xmldoc = new XmlDocument();

  public string XmlFilePath
  {
   set
   {
    xmlfilepath = value ;
   }
  }

  public EnumXmlPathType XmlFilePathtype
  {
   set
   {
    xmlfilepathtype = value ;
   }
  }
  public XmlHelper()
  {
   //
   // TODO: 在此处添加构造函数逻辑
   //
  }
  public XmlHelper( string tempxmlfilepath )
  {
   // todo: 在此处添加构造函数逻辑
   this.XmlFilePathtype = EnumXmlPathType.VirtualPath;
   this.XmlFilePath = tempxmlfilepath ;
   xmldoc=GetXmlDocument();
  }

  public XmlHelper( string tempxmlfilepath , EnumXmlPathType tempxmlfilepathtype )
  {
   // todo: 在此处添加构造函数逻辑
   this.XmlFilePathtype = tempxmlfilepathtype;
   this.XmlFilePath = tempxmlfilepath ;
   xmldoc=GetXmlDocument() ;
  }

  #region 读取xml文件
  // </summary>
  // <param name="strentitytypename">实体类的名称</param>
  // <returns>指定的xml描述文件的路径</returns>
  private XmlDocument GetXmlDocument()
  {
   XmlDocument doc=null;

   if( this.xmlfilepathtype == EnumXmlPathType.AbsolutePath )
   {
    doc = GetXmlDocumentFromFile( xmlfilepath ) ;
   }
   else if( this.xmlfilepathtype == EnumXmlPathType.VirtualPath)
   {
    doc = GetXmlDocumentFromFile(HttpContext.Current.Server.MapPath(xmlfilepath));
   }
   return doc;
  }
  #endregion

  #region 按路径读取xml文件
  private XmlDocument GetXmlDocumentFromFile(string tempxmlfilepath)
  {
   string xmlfilefullpath = tempxmlfilepath ;

   xmldoc.Load(xmlfilefullpath) ;
   return xmldoc ;
  }
  #endregion
 
  #region 读取指定节点的指定属性值
  // <summary>
  // 功能:
  // 读取指定节点的指定属性值
  //
  // 参数:
  // 参数一:节点路径
  // 参数二:此节点的属性
  // </summary>
  // <param name="xmlnodepath"></param>
  // <param name="xmlnodeattribute"></param>
  // <returns></returns>
  
  public string GetXmlNodeAttributeValue(string xmlnodepath,string xmlnodeattribute)
  {
   string strreturn = "";
   try
   {
    //根据指定路径获取节点
    XmlNode xmlnode = xmldoc.SelectSingleNode(xmlnodepath) ;           
    //获取节点的属性,并循环取出需要的属性值
    XmlAttributeCollection xmlattr = xmlnode.Attributes;

    for(int i=0 ;i<xmlattr.Count; i++)
    {
     if (xmlattr.Item(i).Name == xmlnodeattribute)
      strreturn = xmlattr.Item(i).Value;
    }
   }
   catch(XmlException xmle)
   {
    throw xmle ;
   }
   return strreturn ;
  }
  #endregion

  #region 读取指定节点集合第n个节点
  // <summary>
  // 功能:
  // 读取指定节点集合第n个节点的值
  //
  // 参数:
  // 参数:节点集路径
  // </summary>
  // <param name="xmlnodepath"></param>
  // <returns></returns>
  // 参数:
  // 参数:序号
  // </summary>
  // <param name="index"></param>
  // <returns></returns>
  
  public XmlNode GetSubIndexNode(string xmlnodepath,int index)
  {
   XmlNode xmlnode=null;
   try
   {
    //根据路径获取节点
    XmlNodeList nodelist=xmldoc.SelectNodes(xmlnodepath);
    xmlnode = nodelist[index];
   }
   catch(XmlException xmle)
   {
    System.Console.WriteLine(xmle.Message) ;
   }
   return xmlnode;
  }
  #endregion

  #region 读取指定节点的值
  // <summary>
  // 功能:
  // 读取指定节点的值
  //
  // 参数:
  // 参数:节点名称
  // </summary>
  // <param name="xmlnodepath"></param>
  // <returns></returns>
  
  public string GetXmlNodeValue(string xmlnodepath)
  {
   string strreturn = string.Empty;
   try
   {
    //根据路径获取节点
    XmlNode xmlnode = xmldoc.SelectSingleNode(xmlnodepath) ;
    strreturn = xmlnode.InnerText ;
   }
   catch(XmlException xmle)
   {
    System.Console.WriteLine(xmle.Message) ;
   }
   return strreturn ;
  }
  #endregion

  #region 读取指定节点的子节点个数
  // <summary>
  // 功能:
  // 读取指定节点的子节点个数
  //
  // 参数:
  // 参数:节点名称
  // </summary>
  // <param name="xmlnodepath"></param>
  // <returns></returns>
  
  public int GetXmlNodeSubCount(string xmlnodepath)
  {
   int intreturn = 0;
   try
   {
    //根据路径获取节点
    XmlNode xmlnode = xmldoc.SelectSingleNode(xmlnodepath) ;
    intreturn = xmlnode.ChildNodes.Count;
   }
   catch(XmlException xmle)
   {
    System.Console.WriteLine(xmle.Message) ;
   }
   return intreturn ;
  }
  #endregion

  #region 读取指定节点的第index个子节点
  // <summary>
  // 功能:
  // 读取指定节点的子节点个数
  //
  // 参数:
  // 参数:节点名称
  // </summary>
  // <param name="xmlnodepath"></param>
  // <returns></returns>
  
  public XmlNode GetXmlNodeIndexSub(string xmlnodepath,int index)
  {
   XmlNode nodereturn = null;
   try
   {
    //根据路径获取节点
    XmlNodeList list = xmldoc.SelectNodes(xmlnodepath);
    nodereturn=list[index];
   }
   catch(XmlException xmle)
   {
    System.Console.WriteLine(xmle.Message) ;
   }
   return nodereturn ;
  }
  #endregion

  #region 读取指定节点集合节点个数
  // <summary>
  // 功能:
  // 读取指定节点的子节点个数
  //
  // 参数:
  // 参数:节点名称
  // </summary>
  // <param name="xmlnodepath"></param>
  // <returns></returns>
  
  public int GetSubXmlNodeCount(string xmlnodepath)
  {
   int intreturn = 0;
   try
   {
    //根据路径获取节点
    XmlNodeList list = xmldoc.SelectNodes(xmlnodepath) ;
    intreturn = list.Count;
   }
   catch(XmlException xmle)
   {
    System.Console.WriteLine(xmle.Message) ;
   }
   return intreturn ;
  }
  #endregion

  #region 读取指定节点集合第n个节点的值
  // <summary>
  // 功能:
  // 读取指定节点集合第n个节点的值
  //
  // 参数:
  // 参数:节点集路径
  // </summary>
  // <param name="xmlnodepath"></param>
  // <returns></returns>
  // 参数:
  // 参数:序号
  // </summary>
  // <param name="index"></param>
  // <returns></returns>
  
  public string GetSubIndexNodeValue(string xmlnodepath,int index)
  {
   string strreturn = string.Empty;
   try
   {
    XmlNode xmlnode = GetSubIndexNode(xmlnodepath,index);
    strreturn = xmlnode.InnerText ;
   }
   catch(XmlException xmle)
   {
    System.Console.WriteLine(xmle.Message) ;
   }
   return strreturn ;
  }
  #endregion

  #region 读取指定节点集合第n个节点的属性值
  // <summary>
  // 功能:
  // 读取指定节点集合第n个节点的值
  //
  // 参数:
  // 参数:节点集路径
  // </summary>
  // <param name="xmlnodepath"></param>
  // <returns></returns>
  // 参数:
  // 参数:序号
  // </summary>
  // <param name="index"></param>
  // <returns></returns>
  // 参数:
  // 参数:属性名
  // </summary>
  // <param name="xmlnodeattribute"></param>
  // <returns></returns>
  
  public string GetSubIndexNodeAttributeValue(string xmlnodepath,int index,string xmlnodeattribute)
  {
   string strreturn = string.Empty;
   try
   {
    XmlNode xmlnode = GetSubIndexNode(xmlnodepath,index);
    //获取节点的属性,并循环取出需要的属性值
    XmlAttributeCollection xmlattr = xmlnode.Attributes;
    for(int i=0 ;i<xmlattr.Count; i++)
    {
     if (xmlattr.Item(i).Name == xmlnodeattribute)
      strreturn = xmlattr.Item(i).Value;
    }
   }
   catch(XmlException xmle)
   {
    System.Console.WriteLine(xmle.Message) ;
   }
   return strreturn ;
  }
  #endregion

  #region 读取指定节点的子节点中指定属性index为指定属性内容的节点值
  // <summary>
  // 功能:
  // 读取指定节点的子节点中指定属性index为指定属性内容的节点值
  //
  // 参数:
  // 参数:节点集路径
  // </summary>
  // <param name="xmlnodepath"></param>
  // <returns></returns>
  // 参数:
  // 参数:属性序号
  // </summary>
  // <param name="attributeindex"></param>
  // <returns></returns>
  // 参数:
  // 参数:属性值
  // </summary>
  // <param name="attributtevalue"></param>
  // <returns></returns>
  public string GetSubNodeValueByAttributeValue(string xmlnodepath,int attributeindex,string attributtevalue)
  {
   string strreturn = string.Empty;
   try
   {
    XmlNode xmlnode = xmldoc.SelectSingleNode(xmlnodepath);
    XmlNodeList nodelist=xmlnode.ChildNodes;
    XmlNode subnode;
    for(int i=0;i<nodelist.Count;i++)
    {
     subnode=nodelist[i];
     if(subnode.Attributes[attributeindex].Value==attributtevalue)
     {
      strreturn=subnode.Value;
     }
    }
   }
   catch(XmlException xmle)
   {
    System.Console.WriteLine(xmle.Message) ;
   }
   return strreturn ;
  }
  #endregion

  #region 读取指定节点的子节点中指定属性名为指定属性内容的节点值
  // <summary>
  // 功能:
  // 读取指定节点的子节点中指定属性名为指定属性内容的节点值
  //
  // 参数:
  // 参数:节点集路径
  // </summary>
  // <param name="xmlnodepath"></param>
  // <returns></returns>
  // 参数:
  // 参数:属性名
  // </summary>
  // <param name="attributename"></param>
  // <returns></returns>
  // 参数:
  // 参数:属性值
  // </summary>
  // <param name="attributtevalue"></param>
  // <returns></returns>
  public string GetSubNodeValueByAttributeValue(string xmlnodepath,string attributename,string attributtevalue)
  {
   string strreturn = string.Empty;
   try
   {
    XmlNode xmlnode = xmldoc.SelectSingleNode(xmlnodepath);
    XmlNodeList nodelist=xmlnode.ChildNodes;
    XmlNode subnode;
    for(int i=0;i<nodelist.Count;i++)
    {
     subnode=nodelist[i];
     if(subnode.Attributes[attributename].Value==attributtevalue)
     {
      strreturn=subnode.InnerText;
     }
    }
   }
   catch(XmlException xmle)
   {
    System.Console.WriteLine(xmle.Message) ;
   }
   return strreturn ;
  }
  #endregion

  #region 读取指定节点集合第n个节点的第几个属性值
  // <summary>
  // 功能:
  // 读取指定节点集合第n个节点的值
  //
  // 参数:
  // 参数:节点集路径
  // </summary>
  // <param name="xmlnodepath"></param>
  // <returns></returns>
  // 参数:
  // 参数:序号
  // </summary>
  // <param name="index"></param>
  // <returns></returns>
  // 参数:
  // 参数:属性名
  // </summary>
  // <param name="Attributeindex"></param>
  // <returns></returns>
  
  public string GetSubIndexNodeAttributeValue(string xmlnodepath,int index,int attributeindex)
  {
   string strreturn = string.Empty;
   try
   {
    XmlNode xmlnode = GetSubIndexNode(xmlnodepath,index);
    //获取节点的属性,并循环取出需要的属性值
    XmlAttributeCollection xmlattr = xmlnode.Attributes;
    strreturn = xmlattr.Item(attributeindex).Value;
   }
   catch(XmlException xmle)
   {
    System.Console.WriteLine(xmle.Message) ;
   }
   return strreturn ;
  }
  #endregion

  #region 设置节点值
  // <summary>
  // 功能:
  // 设置节点值
  //
  // 参数:
  //    参数一:节点的名称
  //    参数二:节点值
  //   
  // </summary>
  // <param name="xmlnodepath"></param>
  // <param name="xmlnodevalue"></param>
  public void SetXmlNodeValue(string xmlnodepath,string xmlnodevalue)
  {
   try
   {
    //根据指定路径获取节点
    XmlNode xmlnode = xmldoc.SelectSingleNode(xmlnodepath) ;           
    //设置节点值
    xmlnode.InnerText = xmlnodevalue ;
   }
   catch(XmlException xmle)
   {
    throw xmle ;
   }
  }
  #endregion

  #region 设置节点的属性值
  // <summary>
  // 功能:
  // 设置节点的属性值
  //
  // 参数:
  // 参数一:节点名称
  // 参数二:属性名称
  // 参数三:属性值
  //
  // </summary>
  // <param name="xmlnodepath"></param>
  // <param name="xmlnodeattribute"></param>
  // <param name="xmlnodeattributevalue"></param>
  public void SetXmlNodeValue(string xmlnodepath,string xmlnodeattribute,string xmlnodeattributevalue)
  {
   try
   {
    //根据指定路径获取节点
    XmlNode xmlnode = xmldoc.SelectSingleNode(xmlnodepath) ;
           
    //获取节点的属性,并循环取出需要的属性值
    XmlAttributeCollection xmlattr = xmlnode.Attributes ;
    for(int i=0 ; i<xmlattr.Count ; i++)
    {
     if ( xmlattr.Item(i).Name == xmlnodeattribute )
     {
      xmlattr.Item(i).Value = xmlnodeattributevalue;
      break ;
     }
    }
   }
   catch(XmlException xmle)
   {
    throw xmle ;
   }
   SaveXmlDocument();
  }
  #endregion

  #region 获取xml文件的根元素
  // <summary>
  // 获取xml文件的根元素
  // </summary>
  public XmlNode GetXmlRoot()
  {
   return xmldoc.DocumentElement ;
  }
  #endregion

  #region 在根节点下添加父节点
  // <summary>
  // 在根节点下添加父节点
  // </summary>
  public void AddParentNode(string parentnode)
  {
   XmlNode root = GetXmlRoot() ;
   XmlNode parentxmlnode = xmldoc.CreateElement(parentnode) ;
   root.AppendChild(parentxmlnode) ;
  }
  #endregion

  #region 向一个已经存在的父节点中插入一个子节点
  // <summary>
  // 向一个已经存在的父节点中插入一个子节点
  // </summary>
  public void AddChildNode( string parentnodepath,string childnodename )
  {
   XmlNode parentxmlnode = xmldoc.SelectSingleNode(parentnodepath) ;
   XmlNode childxmlnode = xmldoc.CreateElement(childnodename) ;
   parentxmlnode.AppendChild( childxmlnode ) ;
  }
  #endregion

  #region 向一个节点添加属性
  // <summary>
  // 向一个节点添加属性
  // </summary>
  public void AddAttribute( string xmlnodepath , string xmlnodeattribute)
  {
   XmlAttribute nodeattribute = xmldoc.CreateAttribute(xmlnodeattribute) ;
   XmlNode nodepath = xmldoc.SelectSingleNode( xmlnodepath ) ;
   nodepath.Attributes.Append(nodeattribute) ;
  }
  #endregion

  #region 删除一个节点的属性
  // <summary>
  // 删除一个节点的属性
  // </summary>
  public void DeleteAttribute( string strnodepath , string nodeattribute , string nodeattributevalue)
  {
   System.Xml.XmlNodeList nodepath = xmldoc.SelectSingleNode( strnodepath ).ChildNodes;

   foreach(XmlNode xn in nodepath)
   {
    XmlElement xe=(XmlElement)xn;

    if(xe.GetAttribute(nodeattribute)==nodeattributevalue)
    {
     xe.RemoveAttribute(nodeattribute);//删除属性
    }
   }
  }
  #endregion

  #region 删除一个节点
  // <summary>
  // 删除一个节点
  // </summary>
  public void DeleteXmlNode(string tempxmlnode)
  {

   XmlNode xmlnodepath = xmldoc.SelectSingleNode( tempxmlnode ) ;
   xmlnodepath.ParentNode.RemoveChild( xmlnodepath ) ;
  }
  #endregion

  #region 保存xml文件
  // <summary>
  // 功能:
  // 保存xml文件
  //
  // </summary>
  public void SaveXmlDocument()
  {
   try
   {
    //保存设置的结果
    xmldoc.Save( HttpContext.Current.Server.MapPath( xmlfilepath ) );
   }
   catch(XmlException xmle)
   {
    throw xmle;
   }
  }
  #endregion

  #region 保存xml文件
  // <summary>
  // 功能:
  // 保存xml文件
  //
  // </summary>
  public void SaveXmlDocument(string tempxmlfilepath)
  {
   try
   {
    //保存设置的结果
    xmldoc.Save(tempxmlfilepath);
   }
   catch(XmlException xmle)
   {
    throw xmle;
   }
  }
  #endregion
 }
}

 

抱歉!评论已关闭.