简析System.Xml.Linq.dll 程序集
2009-09-22 10:32
337 查看
一、命名空间
System.Xml.Linq.dll 程序集定义了三个命名空间:System.Xml.Linq, System.Xml.Schema 和 System.Xml.XPath
最核心的是 System.Xml.Linq, 定义了对应 XML 文档个方面的很多类型
二、编程方式创建XML文档
以前的 .NET XML编程模型需要使用很多冗长的 DOM API,而 LINQ to XML 则完全可以用与 DOM 无关的方式与 XML 文档交互。这样不但大大减少了代码行,而且这种编程模型可以直接映射到格式良好的XML文档结构。
在内存中创建XML文档
三、使用 LINQ 查询创建XML文档
四、加载和解析XML内容
六、遍历内存中的XML文档
XML示例:
加载
遍历
查询
七、修改 XML文档
System.Xml.Linq.dll 程序集定义了三个命名空间:System.Xml.Linq, System.Xml.Schema 和 System.Xml.XPath
最核心的是 System.Xml.Linq, 定义了对应 XML 文档个方面的很多类型
Member of System.Xml.Linq | Meaning in Life |
XAttribute | Represents an XML attribute on a given XML element |
XComment | Represents an XML comment |
XDeclaration | Represents the opening declaration of an XML document |
XDocument | Represents the entirety of an XML document |
XElement | Represents a given element within an XML document |
XName/XNamespace | Provide a very simple manner to define and reference XML namespaces |
以前的 .NET XML编程模型需要使用很多冗长的 DOM API,而 LINQ to XML 则完全可以用与 DOM 无关的方式与 XML 文档交互。这样不但大大减少了代码行,而且这种编程模型可以直接映射到格式良好的XML文档结构。
static void CreateFunctionalXmlElement() { // A "functional" approach to build an // XML element in memory. XElement inventory = new XElement("Inventory", new XElement("Car", new XAttribute("ID", "1"), new XElement("Color", "Green"), new XElement("Make", "BMW"), new XElement("PetName", "Stan") ) ); // Call ToString() on our XElement. Console.WriteLine(inventory); } |
static void CreateFunctionalXmlDoc() { XDocument inventoryDoc = new XDocument( new XDeclaration("1.0", "utf-8", "yes"), new XComment("Current Inventory of AutoLot"), new XElement("Inventory", new XElement("Car", new XAttribute("ID", "1"), new XElement("Color", "Green"), new XElement("Make", "BMW"), new XElement("PetName", "Stan") ), new XElement("Car", new XAttribute("ID", "2"), new XElement("Color", "Pink"), new XElement("Make", "Yugo"), new XElement("PetName", "Melvin") ) ) ); // Display the document and save to disk. Console.WriteLine(inventoryDoc); inventoryDoc.Save("SimpleInventory.xml"); } |
static void CreateXmlDocFromArray() { // Create an anonymous array of anonymous types. var data = new [] { new { PetName = "Melvin", ID = 10 }, new { PetName = "Pat", ID = 11 }, new { PetName = "Danny", ID = 12 }, new { PetName = "Clunker", ID = 13 } }; // Now enumerate over the array to build // an XElement. XElement vehicles = new XElement("Inventory", from c in data select new XElement("Car", new XAttribute("ID", c.ID), new XElement("PetName", c.PetName) ) ); Console.WriteLine(vehicles); } |
static void LoadExistingXml() { // Build an XElement from string. string myElement = @" Yellow Yugo "; XElement newElement = XElement.Parse(myElement); Console.WriteLine(newElement); Console.WriteLine(); // Load the SimpleInventory.xml file. XDocument myDoc = XDocument.Load("SimpleInventory.xml"); Console.WriteLine(myDoc); } |
XML示例:
<?xml version="1.0" encoding="utf-8"?> <Inventory> <Car carID ="0"> <Make>Ford</Make> <Color>Blue</Color> <PetName>Chuck</PetName> </Car> <Car carID ="1"> <Make>VW</Make> <Color>Silver</Color> <PetName>Mary</PetName> </Car> <Car carID ="2"> <Make>Yugo</Make> <Color>Pink</Color> <PetName>Gipper</PetName> </Car> <Car carID ="55"> <Make>Ford</Make> <Color>Yellow</Color> 862 CHAPTER 24 n PROGRAMMING WITH THE LINQ APIS <PetName>Max</PetName> </Car> <Car carID ="98"> <Make>BMW</Make> <Color>Black</Color> <PetName>Zippy</PetName> </Car> </Inventory> |
static void Main(string[] args) { Console.WriteLine("***** Fun with LINQ to XML *****"n"); // Load the Inventory.xml document into memory. XElement doc = XElement.Load("Inventory.xml"); // We will author each of these next PrintAllPetNames(doc); Console.WriteLine(); GetAllFords(doc); Console.ReadLine(); } |
static void PrintAllPetNames(XElement doc) { var petNames = from pn in doc.Descendants("PetName") select pn.Value; foreach (var name in petNames) Console.WriteLine("Name: {0}", name); } |
static void GetAllFords(XElement doc) { var fords = from c in doc.Descendants("Make") where c.Value == "Ford" select c; foreach (var f in fords) Console.WriteLine("Name: {0}", f); } |
static void AddNewElements(XElement doc) { // Add 5 new purple Fords to the incoming document. for (int i = 0; i < 5; i++) { // Create a new XElement XElement newCar = new XElement("Car", new XAttribute("ID", i + 1000), new XElement("Color", "Green"), new XElement("Make", "Ford"), new XElement("PetName", "") ); // Add to doc. doc.Add(newCar); } // Show the updates. Console.WriteLine(doc); } |
相关文章推荐
- 问题:运行时出现以下错误:出现未处理的“System.Xml.XmlException”类型的异常出现在 system.xml.dll 中
- CLR程序里引用System.Web.dll
- VS2005 .Net项目升级到VS2008后新建Aspx页面找到System.Linq.Dll引用的解决办法
- LINQ to Objects / LINQ to SQL / LINQ to XML 示例程序下载
- 在用c++读取xml文件时,运行时出现以下错误:出现未处理的“System.Xml.XmlException”类型的异常出现在 system.xml.dll 中
- 编译器错误消息: CS0012: 类型“System.Data.Linq.DataContext”在未被引用的程序集中定义。必须添加对程序集“System.Data.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”的引用。
- System.Xml.Linq简介
- 关于安装.NET Framework 2.0时偶然碰到的问题(提示"Error 205015. 安装程序集..../System.EnterpriseServices.dll失败,原因是出现了系统错误:拒绝访问。")
- 类型“System.Xml.Serialization.IXmlSerializable”在未被引用的程序集中定义
- 初识.net界面程序(14)--WPF综合练习(3)LINQ TO XML
- C#程序中使用LINQ to XML来查询XML格式数据的实例
- 未能加载程序集System.EnterpriseServices.dll
- system.dll appwinproc.dll wiaservc.dll恶意程序
- “System.NotSupportedException”类型的异常在 System.Data.Entity.dll 中发生,但未在用户代码中进行处理 其他信息: 在 LINQ to Entitie
- 在 System.InvalidOperationException 中第一次偶然出现的“System.Xml.dll”类型的异常
- JAVA本地程序使用FileSystemXmlApplicationContext读取application-datasorce.xml进行数据库操作
- HtmlAgilityPack System.Xml.XPath.IXPathNavigable”在未被引用的程序集中定义
- CLR程序里引用System.Web.dll
- C#使用System.xml.linq来生成XML文件
- Linq to XML 添加命名空间