LINQ TO XML(一)
1.linq to xml 特性:
一: 以元素位中心 声明模式
二: xml代码结构和生成xml 的代码结构类似
三:支持集成至语言中查询
四:在一条指令中可以创建元素和属性,文本节点仅为字符串 支持简化的xml命名空间
五:快速,精简 ,配合数据流处理
2.linq to xml在内部还是xmlReader xmlWriter ,因此不是一种全新的xml API ,这样还是兼容xmlReader xmlWriter
所以说ling to xml 不但简化xml创建过程,还使得查询xml更容易了,所以ling to xml 应该了解
demo:
/// <summary>
/// 定义一个Book类
/// </summary>
public class Book
{
public string BookName;
public string BookAuthor;
public string BookDate;
}
public Book(string bookname ,string bookauthor, string bookdate)
{
BookName = bookname;
BookAuthor = bookauthor;
BookDate = bookdate;
}
//图书集合
Book[] books = new Book[]{
new Book("Ajax","lin","2010-10-1"),
new Book("Linq","lin","2010-10-2")
};
//基于集合创建xml数据片段
System.Xml.Linq.XElement xml = new System.Xml.Linq.XElement("books",
from book in books
where book.BookName == "Linq"
select new XElement("book",
new XAttribute("name", book.BookName),
new XElement("author", book.BookAuthor),
new XElement("date", book.BookDate)
)
);
Console.WriteLine(xml);