C# Linq to xml学习 用XElement类读写XML文档(用作配置文档)
2012-12-25 16:47
471 查看
Linq to xml就不介绍了,这里重在学习最基本的使用
这里的C#工程和普通的一模一样
需要using System.Xml.Linq;
1.创建XML树(在内存里,如果保存的话,就是xml文档)
上面的程序输出结果如下:
2.添加
下面的示例创建两个 XML 树,然后使用此方法将查询结果添加到其中之一
输出结果如下:
3.查找
4.修改
5.删除
6.将XML读到List,而不是xml树
http://zhidao.baidu.com/question/110134965.html
参考资料:
举例讲解:http://www.cnblogs.com/luckdv/articles/1728088.html?login=1
官方知识:http://msdn.microsoft.com/zh-cn/library/vstudio/system.xml.linq.xelement.aspx
实际运用:http://www.jb51.net/article/22492.htm
这里的C#工程和普通的一模一样
需要using System.Xml.Linq;
1.创建XML树(在内存里,如果保存的话,就是xml文档)
XElement xmlTree1 = new XElement("Root", new XElement("Child1", 1), new XElement("Child2", 2), new XElement("Child3", 3), new XElement("Child4", 4), new XElement("Child5", 5), new XElement("Child6", 6) ); XElement xmlTree2 = new XElement("Root", from el in xmlTree1.Elements() where((int)el >= 3 && (int)el <= 5) select el ); Console.WriteLine(xmlTree2);
上面的程序输出结果如下:
<Root> <Child3>3</Child3> <Child4>4</Child4> <Child5>5</Child5> </Root>
2.添加
下面的示例创建两个 XML 树,然后使用此方法将查询结果添加到其中之一
XElement srcTree = new XElement("Root", new XElement("Element1", 1), new XElement("Element2", 2), new XElement("Element3", 3), new XElement("Element4", 4), new XElement("Element5", 5) ); XElement xmlTree = new XElement("Root", new XElement("NewElement", "Content") ); xmlTree.Add( from el in srcTree.Elements() where (int)el >= 3 select el ); Console.WriteLine(xmlTree);
输出结果如下:
<Root> <NewElement>Content</NewElement> <Element3>3</Element3> <Element4>4</Element4> <Element5>5</Element5> </Root>
3.查找
4.修改
5.删除
6.将XML读到List,而不是xml树
http://zhidao.baidu.com/question/110134965.html
参考资料:
举例讲解:http://www.cnblogs.com/luckdv/articles/1728088.html?login=1
官方知识:http://msdn.microsoft.com/zh-cn/library/vstudio/system.xml.linq.xelement.aspx
实际运用:http://www.jb51.net/article/22492.htm
相关文章推荐
- C# 2008 学习笔记 - LINQ to XML
- C# 2008 学习笔记 - LINQ to XML
- C# Linq To XML的学习(创建并编辑XML树)示例
- C#学习之Linq to Xml
- 参考文章-C# Linq To XML的学习(创建并编辑XML树)示例
- C#学习之Linq to Xml
- C# Linq To XML的学习(创建并编辑XML树)示例
- C# 2008 学习笔记 - LINQ to XML
- C# 2008 学习笔记 - LINQ to XML
- C# 2008 学习笔记 - LINQ to XML
- C# LINQ to XML
- C#中的Linq to Xml详解
- C#基础巩固(2)-Linq To XML创建XML
- C#温故而知新学习系列之XML编程—7-XmlValidatingReader类对XML文档验证
- 用Linq to XML的方式查询XML 文档
- Linq To XML 学习
- .Net那点事儿系列:C#操作Xml:通过XmlDocument读写Xml文档
- LINQ to XML实现XML文档的增删改查
- MyBatis 官方文档学习2---XML 映射配置文件
- LINQ学习心得分享--------(四)LINQ TO XML实用解析