zoukankan      html  css  js  c++  java
  • c#操作xml

      总结一下c#操作xml,之前用的比较多,都是直接数据库的操作xml,今天查看了一些资料,发现c#操作xml也是很方便,有一些方面给sql直接操作xml 更加方便,如增加一些动态的节点,属性。

    先贴一些方法:

     1 //所需要添加的命名空间
     2     using System.Xml;
     3     //初始化一个xml实例
     4     XmlDocument xml=new XmlDocument();
     5     //导入指定xml文件
     6     xml.Load(“xml文件路径path”);
     7     //指定一个节点
     8     XmlNode root=xml.SelectSingleNode("节点名称");
     9     //获取节点下所有直接子节点
    10     XmlNodeList childlist=root.ChildNodes;
    11     //判断该节点下是否有子节点
    12     root.HasChildNodes;
    13     //获取同名同级节点集合
    14     XmlNodeList nodelist=xml.SelectNodes("节点名称");
    15     //生成一个新节点
    16     XmlElement node=xml.CreateElement("节点名称");
    17     //将节点加到指定节点下,作为其子节点
    18     root.AppendChild(node);
    19     //将节点加到指定节点下某个子节点前
    20     root.InsertBefore(node,root.ChildeNodes[i]);
    21     //为指定节点的新建属性并赋值
    22     node.SetAttribute("id","11111");
    23     //为指定节点添加子节点
    24     root.AppendChild(node);
    25     //获取指定节点的指定属性值
    26     string id=node.Attributes["id"].Value;
    27     //获取指定节点中的文本
    28     string content=node.InnerText;
    29     //保存XML文件
    30     xml.Save(“xml文件存储的路径path”);
    View Code

    再贴一些简单的操作xml

    1 XmlDocument xmlDo = new XmlDocument();
    2             xmlDo.LoadXml(xml);
    3 
    4             XmlNode xmlnode = xmlDo.SelectSingleNode("DimensionalCate//dimensional//field["+num+"]");
    5             XmlElement xe = (XmlElement)xmlnode;
    6             string label_value = xe.GetAttribute("label"); //获取节点属性名称
    View Code
  • 相关阅读:
    SpringMVC之文件上传
    Spring之jdbc【继承JdbcDaoSupport】
    Spring中jdbcTemplate的用户实例
    SpringMVC之数据存储
    SpringMVC的日期转换
    SpringMVC配置解决中文乱码的过滤器
    【对数据库操作的封装成工具类之jdbc】
    实现用户注册与登入功能的案例
    【反射之Field】获取字段
    Can't get WebApplicationContext object from ContextRegistry.GetContext(): Resource handler for the 'web' protocol is not defined
  • 原文地址:https://www.cnblogs.com/fgr-lmy/p/4353801.html
Copyright © 2011-2022 走看看