你的位置:首页 > ASP.net教程

[ASP.net教程]C#操作XML的通用方法总结


    在.net的项目开发中,经常会对

1.创建

    /// <summary>    /// 创建/// </summary>    /// <param name="name">根节点名称</param>    /// <param name="type">根节点的一个属性值</param>    /// <returns></returns>       public static string name, string type)    {      try      {        doc = new "<" + name + "/>");        var rootEle = doc.DocumentElement;        rootEle?.SetAttribute("type", type);      }      catch (Exception er)      {        throw new Exception(er.ToString());      }      return doc;    }

2.读取

    /// <summary>    /// 读取数据    /// </summary>    /// <param name="path">路径</param>    /// <param name="node">节点</param>    /// <param name="attribute">属性名,非空时返回该属性值,否则返回串联值</param>    /// <returns>string</returns>    public static string Read(string path, string node, string attribute)    {      var value = "";      try      {        var doc = new var xn = doc.SelectSingleNode(node);        if (xn != null && xn.Attributes != null)          value = (attribute.Equals("") ? xn.InnerText : xn.Attributes[attribute].Value);      }      catch (Exception er)      {        throw new Exception(er.ToString());      }      return value;    }

3.对

    /// <summary>    /// 插入数据    /// </summary>    /// <param name="path">路径</param>    /// <param name="node">节点</param>    /// <param name="element">元素名,非空时插入新元素,否则在该元素中插入属性</param>    /// <param name="attribute">属性名,非空时插入该元素属性值,否则插入元素值</param>    /// <param name="value">值</param>    /// <returns></returns>    public static void Insert(string path, string node, string element, string attribute, string value)    {      try      {        var doc = new var xn = doc.SelectSingleNode(node);        if (element.Equals(""))        {          if (!attribute.Equals(""))          {            var xe = (?.SetAttribute(attribute, value);            //xe?.SetAttribute(attribute, value);          }        }        else        {          var xe = doc.CreateElement(element);          if (attribute.Equals(""))            xe.InnerText = value;          else            xe.SetAttribute(attribute, value);          xn?.AppendChild(xe);        }        doc.Save(path);      }      catch (Exception er)      {        throw new Exception(er.ToString());      }    }

4.修改

    /// <summary>    /// 修改数据    /// </summary>    /// <param name="path">路径</param>    /// <param name="node">节点</param>    /// <param name="attribute">属性名,非空时修改该节点属性值,否则修改节点值</param>    /// <param name="value">值</param>    /// <returns></returns>    public static void Update(string path, string node, string attribute, string value)    {      try      {        var doc = new var xn = doc.SelectSingleNode(node);        var xe = (if (attribute.Equals(""))        {          if (xe != null) xe.InnerText = value;        }        else        {          xe?.SetAttribute(attribute, value);        }        doc.Save(path);      }      catch (Exception er)      {        throw new Exception(er.ToString());      }    }

5.删除

    /// <summary>    /// 删除数据    /// </summary>    /// <param name="path">路径</param>    /// <param name="node">节点</param>    /// <param name="attribute">属性名,非空时删除该节点属性值,否则删除节点值</param>    /// <returns></returns>    public static void Delete(string path, string node, string attribute)    {      try      {        var doc = new var xn = doc.SelectSingleNode(node);        var xe = (if (attribute.Equals(""))        {          xn?.ParentNode?.RemoveChild(xn);        }        else        {          xe?.RemoveAttribute(attribute);        }        doc.Save(path);      }      catch (Exception er)      {        throw new Exception(er.ToString());      }    }

6.读取

    /// <summary>    /// 获得/// </summary>    /// <param name="path"></param>    /// <param name="nodeName"></param>    /// <returns></returns>    public static string GetNodeInfoByNodeName(string path, string nodeName)    {      var string.Empty;      try      {        var new var root = if (root == null) return var node = root.SelectSingleNode("//" + nodeName);        if (node != null)        {          = node.InnerText;        }      }      catch (Exception er)      {        throw new Exception(er.ToString());      }      return 

7.获取

    /// <summary>     /// 功能:读取指定节点的指定属性值       /// </summary>    /// <param name="path"></param>    /// <param name="strNode">节点名称</param>     /// <param name="strAttribute">此节点的属性</param>     /// <returns></returns>     public string Getstring path, string strNode, string strAttribute)    {      var strReturn = "";      try      {        var new //根据指定路径获取节点         var  if (null)        {          //获取节点的属性,并循环取出需要的属性值           var  if (null) return strReturn;          for (var i = 0; i < )          {            if (continue;            strReturn = break;          }        }      }      catch (throw new Exception(return strReturn;    }

8.设置

 /// <summary>     /// 功能:设置节点的属性值       /// </summary>    /// <param name="path"></param>    /// <param name="节点名称</param>     /// <param name="属性名称</param>     /// <param name="属性值</param>     public void Setstring path, string string string try      {        var new //可以批量为符合条件的节点的属性付值         var  if (null) return;        foreach (var in from in select xn.Attributes)        {          if (null) return;          for (var i = 0; i < )          {            if (continue;            = break;          }        }      }      catch (throw new Exception(

9.读取

    /// <summary>    /// 读取/// </summary>    /// <param name="source"></param>    /// <param name="</param>    /// <param name="nodeName">节点名称</param>    /// <returns>节点内容</returns>    public static object GetNodeValue(string source, string nodeName)    {      var xd = new if ( else      {        xd.Loadvar xe = xd.DocumentElement;      = null;      if (xe != null)      {         xn= xe.SelectSingleNode("//" + nodeName);              }      return xn.InnerText;    }

10.更新

    /// <summary>    /// 更新/// </summary>    /// <param name="filePath">文件路径</param>    /// <param name="nodeName">节点名称</param>    /// <param name="nodeValue">更新内容</param>    /// <returns>更新是否成功</returns>    public static bool UpdateNode(string filePath, string nodeName, string nodeValue)    {            try      {        bool flag;        var xd = new var xe = xd.DocumentElement;        if (xe == null) return false;        var xn = xe.SelectSingleNode("//" + nodeName);        if (xn != null)        {          xn.InnerText = nodeValue;          flag = true;        }        else        {          flag = false;        }        return flag;      }      catch (Exception ex)      {        throw new Exception(ex.Message);      }          }

11.将对象转化为

    /// <summary>    /// 将对象转化为/// </summary>    /// <typeparam name="T">C#对象名</typeparam>    /// <param name="item">对象实例</param>    /// <param name="path">路径</param>    /// <param name="jjdbh">标号</param>    /// <param name="ends">结束符号(整个</param>    /// <returns></returns>    public static string Writestring path, string jjdbh, string ends)    {      if (string.IsNullOrEmpty(ends))      {        //默认为发送        ends = "send";      }      //控制写入文件的次数      var i = 0;      //获取当前对象的类型,也可以使用反射typeof(对象名)      var serializer = new //      object[] obj = { path, "\\", jjdbh, ends, "." };      var string.Concat(obj);      while (true)      {        try        {          //用filestream方式创建文件不会出现“文件正在占用中,用File.create”则不行          var fs = System.IO.File.Create(= new StreamWriter(false, Encoding.UTF8);          var new string.Empty, string.Empty);          serializer.Serialize(writer, item, break;        }        catch (Exception)        {          if (i < 5)          {            i++;            continue;          }          break;        }      }      return SerializeTotrue);    }

12.向一个已经存在的父节点中插入一个子节点:

    /// <summary>     /// 向一个已经存在的父节点中插入一个子节点     /// </summary>    /// <param name="path"></param>    /// <param name="parentNodePath">父节点</param>    /// <param name="childnodename">子节点名称</param>     public void AddChildNode(string path, string parentNodePath, string childnodename)    {      try      {        var new var parent = if ((parentnull)        {          //如果此节点存在            parentelse        {          //如果不存在就放父节点添加           Getcatch (throw new Exception(

   以上的方法总结采用.net4.5版本和c#6.0语法