using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
XDocument doc = XDocument.Load("XMLFile1.xml");
var queryBook = from q in doc.Root.Descendants("book")
select new Book
{
Title = q.Element("title").Value,
Author = q.Element("author").Value,
Price = Decimal.Parse(q.Element("price").Value)
};
List<Book> bk = queryBook.ToList();
foreach (XElement item in doc.Root.Descendants("book"))//得到每一个Person节点,得到这个节点再取他的Name的这个节点的值
{
Console.WriteLine(item.Element("title").Value);//Person的节点的下得节点为Name的
}
Console.WriteLine(queryBook.Sum(q=>q.Price));
Console.WriteLine();
}
}
}
===========================================
<?xml version="1.0" encoding="utf-8" ?>
<bookstore>
<book genre="fantasy" ISBN="2-3631-4">
<title>Oberon's Legacy</title>
<author>Corets, Eva</author>
<price>5.95</price>
</book>
<book genre="fantasy" ISBN="2-3631-4">
<title>Oberon's Legacy</title>
<author>Corets, Eva</author>
<price>6</price>
</book>
</bookstore>