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
XML操作之Linq to Xml的更多相关文章
- Linq to xml 操作带命名空间的xml
昨天需要操作用代码操作csproj文件,实现不同vs版本的切换. 在用XElement读取了csproj文件以后怎么也获取不到想要的对象. 反反复复试验了好多次都不得要领:先看下csproj文件的内容 ...
- 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 ...
- C#操作Xml:linq to xml操作XML
LINQ to XML提供了更方便的读写xml方式.前几篇文章的评论中总有朋友提,你为啥不用linq to xml?现在到时候了,linq to xml出场了. .Net中的System.Xml.Li ...
- linq to xml 简单的增、删、改、查、保存xml文件操作
using System; using System.Collections; using System.Configuration; using System.Data; using System. ...
- Linq之Linq to XML
目录 写在前面 系列文章 linq to xml 总结 写在前面 在很多情况下,都可以见到使用xml的影子.例如,在 Web 上,在配置文件.Microsoft Office Word 文件(将wor ...
- Linq世界走一走(LINQ TO XML)
前言:Linq to xml是一种使用XML的新方法.从本质上来说,它采用了多种当前使用的XML处理技术,如DOM和XPath,并直接在.NET Framework内将它们组合为一个单一的编程接口.L ...
- C#操作Xml:使用XmlReader读Xml
XmlDocument和XElement在读取Xml时要将整个Xml文档放到内存中去操作,这样做操作简单,但是很费内存和IO(可能是磁盘IO或者网络IO):而在有些场景下我们必须考虑尽可能节省内存和I ...
- LINQ to XML基本操作
Linq to XML同样是对原C#访问XML文件的方法的封装,简化了用xpath进行xml的查询以及增加,修改,删除xml元素的操作. LINQ to XML 三个最重要类:XElement.XAt ...
- C#使用Linq to XML进行XPath查询
最近在用到HtmlAgliltyPack进行结点查询时,发现这里选择结点使用的是XPath.所以这里总结一下在C#中使用XPath查询XML的方法.习惯了用Linq,这里也是用的Linq to xml ...
随机推荐
- DVB系统中PCR的生成和PCR校正
http://blog.csdn.net/chenliangming/article/details/3616720 引自<广播电视信息>2008年1月 从数字电视前端系统功能上来讲,传统 ...
- 12232 - Exclusive-OR
12232 - Exclusive-OR 题目大意是可以设定一个点Xp=v,或者Xp^Xq=v,然后查询Xa^Xb^Xc...等于多少. 由于异或操作跟判连通性很类似,这里可以使用并查集来解决,对于X ...
- flask开发遇到Internal Server Error的解决办法
flask开发过程中遇到了Internal Server Error错误,可以在代码加上debug app.debug=True 这样就能看到错误信息了
- Android-xUtils框架介绍(三)
继续介绍xUtils的最后两个模块:DbUtils和HttpUtils.首先先介绍第一个SQLite数据库操纵的简单ORM框架,只要能理解xUtils为我们提供的api,相信你也能熟练的把DbUtil ...
- poj2482Stars in Your Window(线段树+离散化+扫描线)
http://poj.org/problem?id=2482 类似于上一篇 这题转化的比较巧妙 将一个点转化为一个矩形(x,y, x+w,y+h),扫描线入值为正 出值为负 也就是一根线过去 每进入一 ...
- iScroll-5拉动刷新功能实现与iScroll-4上拉刷新的一点改进
近来在学习移动设备的应用开发,接触了jQuery mobile,在网上查阅相关资料时发现一个叫”iScroll“的小插件.其实这个iScroll插件跟jQuery mobile没有多大关系,并不是基于 ...
- JS 打印报表
<script type="text/javascript"> window.print(); </script> 前台页面: <%@ Page La ...
- Imageview使用记录
1. imageView清除背景 原文网址:http://blog.csdn.net/lzq1039602600/article/details/40393591 两种清除 imageView的背景 ...
- 【转】Android中visibility属性VISIBLE、INVISIBLE、GONE的区别
原文网址:http://blog.csdn.net/chindroid/article/details/8000713 在Android开发中,大部分控件都有visibility这个属性,其属性有3个 ...
- c#编程指南(四) 组元(Tuple)
(1).C#语法中一个个问号(?)的运算符是指:可以为 null 的类型. MSDN上面的解释: 在处理数据库和其他包含不可赋值的元素的数据类型时,将 null 赋值给数值类型或布尔型以及日期类型的功 ...