Linq to Xml
2016-06-03 17:48
323 查看
使用linq to xml 创建xml文件,查询xml文件中的信息
未完待续·····生成的XML文件的内容如下:
<?xml version="1.0" encoding="utf-8"?> <Users> <User ID="111111"> <name>EricSun</name> <password>123456</password> <description>Hello I'm from Dalian</description> </User> <User ID="222222"> <name>Ray</name> <password>654321</password> <description>Hello I'm from Jilin</description> </User> </Users>
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Xml; using System.Linq; using System.Xml.Linq; using System.Collections; namespace ConsoleApplication6 { class StudyXml { static void Main(string[] args) { string xmlpath = "Created.xml"; //第一个方法测试 // GenerateXmlFile(xmlpath); //第二个方法测试 // GetXmlNodeInfo(xmlpath); //第三个方法测试 GetNodeInfoLinq(xmlpath); Console.ReadKey(); } #region 生成一个xml文件 private static void GenerateXmlFile(string pathName) { try { XDocument xd = new XDocument( new XElement("Users", new XElement("User", new XAttribute("ID", "111111"), new XElement("name", "EricSun"), new XElement("password", "123456"), new XElement("description", "Hello I'm from Dalian") ), //第三个 new XElement("User", new XAttribute("ID", "222222"), new XElement("name", "Ray"), new XElement("password", "654321"), new XElement("description", "Hello I'm from Jilin") ) ) ); xd.Save(pathName); } catch (Exception e) { Console.WriteLine(e.ToString()); } } #endregion #region 如何获取xml中每个节点的信息 private static void GetXmlNodeInfo(string xmlpath) { //加载xml文件 XDocument document = XDocument.Load(xmlpath); //获取“User”节点 XElement rootele = document.Element("Users"); //遍历此节点下的属性和指定元素的值 foreach(XElement node in rootele.Elements("User")) { Console.WriteLine(node.Attribute("ID").Value); string name = node.Element("name").Value; string password = node.Element("password").Value; string discription = node.Element("description").Value; Console.WriteLine("{0}:{1}:{2}",name,password,discription); } } #endregion #region private static void GetNodeInfoLinq(string path) { XDocument xdocument = XDocument.Load(path); #region IEnumerable接口就一个方法,没有属性。 // GetEnumerator 返回一个循环访问集合的枚举数。 实现或继承了该接口,就是为了这个方法,可以foreach遍历。 #endregion //查询元素名为name的值(xml中有两个元素的名都为name) IEnumerable<XElement> ie = from nodes in xdocument.Descendants("name") select nodes; foreach (XElement xd in ie) { Console.WriteLine(xd.Value); } IEnumerable<XElement> ie1 = from nodes1 in xdocument.Descendants("User") where nodes1.Attribute("ID").Value.Equals("111111") && nodes1.HasElements select nodes1; foreach(XElement xe in ie1) { Console.WriteLine("this is the second"); Console.WriteLine(xe.Element("name").Value); } } #endregion } }
相关文章推荐
- 事务
- android下数据库的增删改查
- [原创]java WEB学习笔记38:EL 中的 11个 隐含对象 详解
- java中的集合
- 关于坚持使用C/C++语言的原因
- ffmpeg系列(2) ffmpeg编解码及过滤器的运行
- FreeMarker语法知识
- json对象两个方法的解析
- 开发一流的 Android SDK: Fabric SDK 的创建经验
- My Library
- 第四章 字典:当索引不好用时
- 如何用vs查看结构体布局
- 初学Maven所报的各种错
- android下数据库的创建
- 图文详解YUV420数据格式
- android 计步器代码(prdometer)
- Eclipse常用快捷键及快捷键功能失效时的解决方案
- 搭建 GIT 服务器
- C++11 | 自动类型推断——auto
- Linux下搭建嵌入式交叉编译环境