需要引用的命名空间:   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相关随笔

XML操作之Linq to Xml的更多相关文章

  1. Linq to xml 操作带命名空间的xml

    昨天需要操作用代码操作csproj文件,实现不同vs版本的切换. 在用XElement读取了csproj文件以后怎么也获取不到想要的对象. 反反复复试验了好多次都不得要领:先看下csproj文件的内容 ...

  2. xml操作-Nested exception: org.xml.sax.SAXParseException: White spaces are required between publicId and systemId. 异常处理

    异常如下: org.dom4j.DocumentException: Error on line 2 of document file:///D:/workspaces/struts2/lesson0 ...

  3. C#操作Xml:linq to xml操作XML

    LINQ to XML提供了更方便的读写xml方式.前几篇文章的评论中总有朋友提,你为啥不用linq to xml?现在到时候了,linq to xml出场了. .Net中的System.Xml.Li ...

  4. linq to xml 简单的增、删、改、查、保存xml文件操作

    using System; using System.Collections; using System.Configuration; using System.Data; using System. ...

  5. Linq之Linq to XML

    目录 写在前面 系列文章 linq to xml 总结 写在前面 在很多情况下,都可以见到使用xml的影子.例如,在 Web 上,在配置文件.Microsoft Office Word 文件(将wor ...

  6. Linq世界走一走(LINQ TO XML)

    前言:Linq to xml是一种使用XML的新方法.从本质上来说,它采用了多种当前使用的XML处理技术,如DOM和XPath,并直接在.NET Framework内将它们组合为一个单一的编程接口.L ...

  7. C#操作Xml:使用XmlReader读Xml

    XmlDocument和XElement在读取Xml时要将整个Xml文档放到内存中去操作,这样做操作简单,但是很费内存和IO(可能是磁盘IO或者网络IO):而在有些场景下我们必须考虑尽可能节省内存和I ...

  8. LINQ to XML基本操作

    Linq to XML同样是对原C#访问XML文件的方法的封装,简化了用xpath进行xml的查询以及增加,修改,删除xml元素的操作. LINQ to XML 三个最重要类:XElement.XAt ...

  9. C#使用Linq to XML进行XPath查询

    最近在用到HtmlAgliltyPack进行结点查询时,发现这里选择结点使用的是XPath.所以这里总结一下在C#中使用XPath查询XML的方法.习惯了用Linq,这里也是用的Linq to xml ...

随机推荐

  1. 选择排序的MPI实现

    #include "stdafx.h" #include "mpi.h" #include <stdio.h> #include <math. ...

  2. 哈希值识别工具hash-identifier

    Hash Identifier可以用来识别各种类型的哈希值.在kali上使用方法很简单 (1)搜索hash-identifier (2)在HASH后面输入要识别的hash内容 (3)识别成功 wind ...

  3. 深入理解Android内存管理原理(六)

    一般来说,程序使用内存的方式遵循先向操作系统申请一块内存,使用内存,使用完毕之后释放内存归还给操作系统.然而在传统的C/C++等要求显式释放内存的编程语言中,记得在合适的时候释放内存是一个很有难度的工 ...

  4. poj 2503 Babelfish (查找 map)

    题目:http://poj.org/problem?id=2503 不知道为什么 poj  的 数据好像不是100000,跟周赛的不一样 2000MS的代码: #include <iostrea ...

  5. 微软 Virtual studion Code

    在 Build 2015 大会上,微软除了发布了 Microsoft Edge 浏览器和新的 Windows 10 预览版外,最大的惊喜莫过于宣布推出免费跨平台的 Visual Studio Code ...

  6. apache开源项目--ZooKeeper

    ZooKeeper是Hadoop的正式子项目,它是一个针对大型分布式系统的可靠协调系统,提供的功能包括:配置维护.名字服务.分布式同步.组服务等.ZooKeeper的目标就是封装好复杂易出错的关键服务 ...

  7. JAVA方法和本地方法(转载)

    转载自:http://blog.sina.com.cn/s/blog_5b9b4abe01016zw0.html JAVA中有两种方法:JAVA方法和本地方法   JAVA方法是由JAVA编写的,编译 ...

  8. WCF学习笔记(二):简单调用

    转:http://www.cnblogs.com/wengyuli/archive/2009/11/08/1598428.html 一个通信会话过程有两个部分组成,客户端和服务端,他们要进行会话就必然 ...

  9. 序列化类型为XX的对象时检测到循环引用

    /// 产品列表展示 /// </summary> /// <returns></returns> ) { //获得所有组别 Galasys_IBLL.IT_BIZ ...

  10. HDU 5288 OO’s Sequence

    题意:给一个序列,函数f(l, r)表示在[l, r]区间内有多少数字不是其他数字的倍数,求所有区间的f(l, r)之和. 解法:第一次打多校……心里还有点小激动……然而一道签到题做了俩点……呜呜呜… ...