zoukankan      html  css  js  c++  java
  • XML操作之Linq to Xml

    需要引用的命名空间:   using System.Xml.Linq;

    常用的类:XDocument、XElement、XAttribute

    创建 XDocument对象。

    • XDocument.Load():从文件、URI 或流中读取 XML 文档
    • XDocument.Parse():从一个字符串加载 XML 文档

    一、使用Linq to xml创建xml文档

                XDocument xml = new XDocument(
                    new XElement("configuration",
                        new XElement("ImgButtonSettings",
                            new XElement("button",
                                new XElement("name", new XAttribute("id", "EFS"), "close"),
                                new XElement("size-w",61),
                                new XElement("size-h", new XAttribute("ff", 564), 56),
                                new XElement("localtion-x",970),
                                new XElement("localtion-y",3),
                                new XElement("openurl",""),
                                new XElement("visable",true)
                                ),
                             new XElement("button",
                                new XElement("name", new XAttribute("id", "EFS"), "back"),
                                new XElement("size-w", 61),
                                new XElement("size-h", new XAttribute("ff", 564), 56),
                                new XElement("localtion-x", 990),
                                new XElement("localtion-y", 3),
                                new XElement("openurl", ""),
                                new XElement("visable", true)
                                )
                         )
                    )
                );
                xml.Save(@"E:123.xml");
    

      

    二、使用Linq to xml 查询xml

    注意子元素和子代(即后代)元素的区别,子元素就是儿子 ,子代元素就是所有后代

    Element()和Elements()方法获取的都是子元素,非子元素的后代元素是获取不到的

    Descendants()获取的是后代元素

    XDocument对象的子元素有且只有一个就是xml的根节点

      // 获取button节点下的所有localtion-x节点
                var node = from x in xml.Descendants("button").Elements()
                           where x.Name == "localtion-x"
                           select x;
                foreach (var item in node)
                {
                    Console.WriteLine(item.Name);//获取节点的名字
                    Console.WriteLine(item.Value);//获取节点的值
                }
    
    
                //获取button下id属性为name的所有name节点
                var node2 = from x in xml.Descendants("button").Elements("name")
                           where x.Attribute("id").Value == "ABC"
                           select x;
                foreach (var item in node2)
                {
                    Console.WriteLine(item.Value);
                }
                

     传统的XML读取方式:http://www.cnblogs.com/lxf1117/p/4178678.html

    .Net处理Xml相关随笔

  • 相关阅读:
    编程语言是一种宗教
    execel 的java库
    c3p0配置学校
    Linux 文件命令精通指南
    几个WEB中常用的js方法
    不可多得的Javascript(AJAX)开发工具 - Aptana
    JDBC连不上Oracle数据库的解决方法
    利用PROFILE管理口令和资源
    ORACLE 数据库名、实例名、ORACLE_SID的区别
    用Java编写Oracle存储过程
  • 原文地址:https://www.cnblogs.com/lxf1117/p/4178510.html
Copyright © 2011-2022 走看看