首页 > 技术文章 > Linq第一篇

mhdtysp 2015-08-11 01:05 原文

1.Linq to xml:

命名空间:System.XML

类 XElement

用XElment类构造XML树:

可以在代码中构造xml树

可以从包括TextReader,文本文件或者web地址(URL)在内的各种源解析XML

可以使用XmlReader来填充树

 

XAttribute类

XAttribute类表示XML属性

属性是与元素关联的名称/值对

使用LINQ to XML中的属性,与使用元素非常相似。他们的构造函数相似。用于检索他们的集合的方法相似。属性集合的LINQ查询表达式与元素集合的LINQ查询表达式看起来非常相似。

将属性添加到元素中的顺序会保留下来。也就是说,当循环访问属性时,缩减到的属性顺序会保留下来。

 

XDocument类

XDocumnet类包含有效的XML文档所需要的信息。其中包括XML声明,处理指令和注释。

如果需要XDocument类提供的特地给功能,只需要创建XDocument对象。在很多情况下,可以直接使用XElement。直接使用XElement是一种比较简单的编程模型。

XDocument对象只能有一个子XElement节点。这反映了XML标准,即在XML文档中只能有一个根元素。

 

(vs自动代码整理ctrl+a+k+f,a,k,f要轮流按,不要同时按下,a是为了全部选中代码,也可以选中想要整理的代码,然后k,f)

XML名称常常是导致XML编程复杂性的原因,XML名称由XML命名空间(也成为XML命名空间URI)和本地名称组成。XML命名空间的作用与给予.NET Framework的程序中命名空间的作用类似,它能够唯一限定元素和属性的名称。这有助于避免XML问大概各个部分之间的名称冲突。声明XML命名空间后,可以选择只需在此命名空间内唯一的本地名称。

 

序列化XML树

XElement和XDocument类中的以下方法可以用于序列化XML树。可以将xml树序列化为文件,textreader或xmlreader.ToString方法序列化为字符串

XElement.Save

XDocument.Save

XElement.ToString

XDocument.ToString

SaveOptions

DisableFormatting:序列化时保留无关紧要的空白

None:不对格式做处理

  1 using System;
  2 using System.Collections.Generic;
  3 using System.IO;
  4 using System.Linq;
  5 using System.Text;
  6 using System.Threading.Tasks;
  7 using System.Xml;
  8 using System.Xml.Linq;
  9 
 10 namespace LinqConsole
 11 {
 12     class Program
 13     {
 14         static void Main(string[] args)
 15         {
 16             #region demo1
 17             //XDocument contacts = new XDocument(
 18             //        new XElement("Contacts",
 19             //        new XComment("This is a comment."),
 20             //           new XProcessingInstruction("xml-stylesheet",
 21             //               "href='mystyle.css' title='Compact' type='text/css'"),
 22             //           new XElement("Contact",
 23             //               new XElement("Name",
 24             //               new XAttribute("Type", "Name"), "Patrick Hines"),
 25             //               new XElement("Phone", "206-201-0250"),
 26             //               new XElement("Address",
 27             //                   new XElement("Street", "123 Main St"),
 28             //                   new XElement("City", "Mercer Island"),
 29             //                   new XElement("State", "WA"),
 30             //                   new XElement("Postal", "52640")
 31             //               )
 32             //               ),
 33             //           new XComment("This is another conment.")
 34 
 35             //           )
 36             //       );
 37 
 38             //contacts.Declaration = new XDeclaration("1.0", "utf-8", "true");
 39             //Console.WriteLine(contacts);
 40 
 41             //contacts.Save("test.xml");
 42             //Console.ReadKey();
 43             #endregion
 44 
 45             #region demo2
 46             //XElement srcTree =
 47             //   new XElement("Root",
 48             //       new XElement("XElement",1),
 49             //       new XElement("XElement", 2),
 50             //       new XElement("XElement", 3),
 51             //       new XElement("XElement",4),
 52             //       new XElement("XElement", 5)
 53             //       );
 54 
 55             //XElement xmlTree =
 56             //  new XElement("Root",
 57             //      new XElement("Child", 1),
 58             //      new XElement("Child", 2),
 59             //      from item in srcTree.Elements()
 60             //      where (int)item>2
 61             //      select item
 62             //      );
 63 
 64             //Console.WriteLine(xmlTree);
 65             //Console.ReadKey();
 66             #endregion
 67 
 68             #region demo3
 69             //XmlReader r = XmlReader.Create("books.xml");
 70             //while (r.NodeType != XmlNodeType.Element)
 71             //{
 72             //    r.Read();
 73             //}
 74             //XElement e1 = XElement.Load(r); //也可以直接load文档
 75             //Console.WriteLine(e1);
 76             //Console.ReadKey();
 77             #endregion
 78 
 79             #region demo4
 80             //XNamespace aw = "http://www.adventure-works.com";
 81             //XNamespace fc = "www.fourthooffee.com";
 82             //XElement root = new XElement( "Root",
 83             //    new XAttribute(XNamespace.Xmlns + "aw", "http://www.adventure-works.com"),
 84             //    new XAttribute(XNamespace.Xmlns+"fc", "www.fourthooffee.com"),
 85             //    new XElement(fc + "Child", 
 86             //        new XElement(aw+"DifferentChild","other content")              
 87             //    ),
 88             //    new XElement(aw+"Child2","c2 content"),
 89             //    new XElement(fc+"Child3","c3 content")
 90             //);
 91             //Console.WriteLine(root);
 92             //Console.ReadKey();
 93             #endregion
 94 
 95             #region demo5
 96             //带xml声明的序列化
 97             //XElement root = new XElement("Root",
 98             //    new XElement("Child", "child content"));
 99             //root.Save("Root.xml",SaveOptions.DisableFormatting);
100             //string str = File.ReadAllText("Root.xml");
101             //Console.WriteLine(str);
102             //Console.ReadKey();
103 
104             //不带xml声明的序列化
105             StringBuilder sb = new StringBuilder();
106             XmlWriterSettings xws = new XmlWriterSettings();
107             xws.OmitXmlDeclaration = true;
108             using (XmlWriter xw = XmlWriter.Create(sb, xws))
109             {
110                 XElement root1 = new XElement("Root",
111                     new XElement("Child", "child content"));
112                 root1.Save(xw);
113             }
114 
115             Console.WriteLine(sb.ToString());
116             Console.ReadKey();
117             #endregion
118 
119         }
120     }
121 }
View Code

 

推荐阅读