Linq To XML
一、 LINQ To XML常用类
-
Linq To XML类的层次结构图:
2.抽象类基类:XObject
3.抽象基类:XNode
4.抽象基类:XContainer
5.XElement:
7.XAttribute
二. Loading XML
XML能够被加载从文件系统中、URL和XMLReader中,通常的处理方式是采用XElement类的静态方法Load进行处理。例如:XElement element = XElement.Load("book.xml");
在某些特殊情况下,XML文件中可能包含有某些空白字符【WihteSpace】,在加载过程中,根据不同的需求,可能过滤掉WithteSpace或者保留WitheSpace。可以运用Load的重载方法进行处理。如下所示:
XElement element = XElement.Load("book.xml",LoadOptions.PreserveWhitespace); 枚举LoadOptions类结构图如下所示:
从URL中加载XML采用同样的处理方式:
string xmlUrl = "http://msdn.microsoft.com/rss.xml"; XElement xelment = XElement.Load(xmlUrl, LoadOptions.PreserveWhitespace);
在某些特殊情况下,XML可能是内存中的字符串,完全可以采用Parse方法将带有XML格式的字符串转化为XML。
XElement x = XElement.Parse( @"<books> <book> <author>Don Box</author> <title>Essential .NET</title> </book> <book> <author>Martin Fowler</author> <title>Patterns of Enterprise application architecture</title> </book> </books>");
三.Create XML
创建XML最简单的处理方式是调用函数式结构创建XML文档。掉XElement构造函数进行创建,构造函数如下所示:
Public XElement(XName name) Public XElement(XName name,object content) Public XElement(XName name,paras object[] content)
例如:
XElement book = new XElement("book"); XElement name = new XElement("name", "Steve Eichert"); XElement books = new XElement("books", new XElement("book", "LINQ in Action"), new XElement("book", "Ajax in Action") );
四.Adding Content to XML
在日常的XML处理当中,可能会面临个XML节点添加属性、能容、子节点等操作。LING To XML 提供了强有力的API,以帮助Developer快速开发。常用API如下:
Public void Add(object content) Public void Add(params object[] content)
例如:添加元素
XElement book = new XElement("book"); book.Add(new XElement("author", "Dr.Seuss"));
输出:
例如:添加属性
XElement book = new XElement("book"); book.Add(new XAttribute("publicationDate","October 2005"));
输出:
例如:添加元素、属性
XElement books = new XElement("books"); books.Add(new XElement("book", new XAttribute("publicationDate", "May 2006"), new XElement("author", "Chris Sells"), new XElement("title", "Windows Forms Programming") ) );
输出:
在日常工作中,可能还会经常使用如下几个添加XML节点的方法,AddFirst、AddAfterSelf、AddBeforeSelf。下面详细介绍其用法,请注意观察输出
Number One:Add
XElement root = new XElement("Root"); XElement firstChildren = new XElement("FirstChildren", "FirstChildren"); root.Add(firstChildren); //as the first children node of the root node
输出:
Number Two:AddFirst
XElement root = new XElement("Root"); XElement firstChildren = new XElement("FirstChildren", "FirstChildren"); root.Add(firstChildren); //as the first children node of the root node XElement firstNode = new XElement("FirstNode", "FirstNode"); root.AddFirst(firstNode);
输出:
Number Three:AddBeforeSelf
XElement root = new XElement("Root"); XElement firstChildren = new XElement("FirstChildren", "FirstChildren"); root.Add(firstChildren); //as the first children node of the root node XElement firstNode = new XElement("FirstNode", "FirstNode"); root.AddFirst(firstNode); XElement beforeFirstNode = new XElement("BeforeFirstNode", "BeforeFirstNode"); firstNode.AddBeforeSelf(beforeFirstNode); // call the addbeforeself method
Number Four:AddAfterSelf
XElement root = new XElement("Root"); XElement firstChildren = new XElement("FirstChildren", "FirstChildren"); root.Add(firstChildren); //as the first children node of the root node XElement firstNode = new XElement("FirstNode", "FirstNode"); root.AddFirst(firstNode); XElement beforeFirstNode = new XElement("BeforeFirstNode", "BeforeFirstNode"); firstNode.AddBeforeSelf(beforeFirstNode); // call the addbeforeself method XElement afterFirstNode = new XElement("AfterFirstNode", "AfterFirstNode"); firstNode.AddAfterSelf(afterFirstNode);
Remove content from XML
books.Element("Book").Remove(); //Remove the first book books.Elements("Book").Remove(); //Remove all books 或者 Books.SetElmentValue(“book”,null);
Update XML content
XML中简单的文本元素更新可以调用SetElementValue方法。例如:源XML文档:
<books> <book> <title>LINQ in Action</title> <author>Steve Eichert</author> </book>
</books>
更新author节点:
XElement element = XElement.Parse( @"<books> <book> <title>LINQ in Action</title> <author>Steve Eichert</author> </book> </books>" );
element.Element("book").SetElementValue("author", "Bill Gates");
输出:
对SetElementValue方法而言,只能够用文本更新叶子节点的内容,不能够用元素更新叶子节点的内容,如果更新,出现:
如果要用节点元素更新节点的内容,可以采用ReplaceNodes方法:
XElement element = XElement.Parse( @"<books> <book> <title>LINQ in Action</title> <author>Steve Eichert</author> </book> </books>" ); element.Element("book").Element("author").ReplaceNodes(new XElement("Name", "Bill Gates"));
输出:
当要替换掉某个XML节点,包括节点本身页面被替换,可以采用ReplaceWith方法,SetElementValue,ReplaceNodes仅仅更新节点内容。例如:
XElement element = XElement.Parse( @"<books> <book> <title>LINQ in Action</title> <author>Steve Eichert</author> </book> </books>" ); element.Element("book").Element("author").ReplaceWith(new XElement("Name", "Bill Gates"));
输出:
Working with attribute
Public XAttribute(XName name,object value)
添加属性:
Book.add(new XAttribute(“pubDate”,”July 31,2006”));
修改属性:
Book.SetAttributeValue(“pubDate”,”October 1,2006”));
删除属性:
Book.Attribute(“pubDate”).Remove();
Saving XML
XElement books = new XElement("books", new XElement("book", new XElement("title", "LINQ in Action"), new XElement("author", "Steve Eichert"), new XElement("author", "Jim Wooley"), new XElement("author", "Fabrice Marguerie") ) ); books.Save(@"c:/books.xml");