Linq To XML
今天没时间写注释,呵呵,以后加上,put on my codes first:
代码
/*
* Author: sandals
* Date : 2010-08-07 11:24
* Description: Demo for System.Xml.Linq
* */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
namespace Sandals.LinqDemo
{
class Program
{
static void Main(string[] args)
{
//// Creat a new xml document
XDocument xDoc = new XDocument();
xDoc.Declaration = new XDeclaration("1.0", "utf-8", "yes");
XComment xComment = new XComment("This is sandals' test demo");
//// add a new element to XDocument
xDoc.Add(xComment);
XElement bookstore = new XElement("bookstore");
xDoc.Add(bookstore);
XElement book = new XElement("book",
new XAttribute("category", "COOKING"),
new XElement("title",
new XAttribute("lang", "en"),
"Everyday Italian"
),
new XElement("author","Giada De Laurentiis"),
new XElement("year","2005"),
new XElement("price", "30")
);
bookstore.Add(book);
book = new XElement("book",
new XAttribute("category", "CHILDREN"),
new XElement("title",
new XAttribute("lang", "en"),
"Harry Potter"
),
new XElement("author", "J K. Rowling"),
new XElement("year", "2005"),
new XElement("price", "29.99")
);
bookstore.Add(book);
book = new XElement("book",
new XAttribute("category", "WEB"),
new XElement("title",
new XAttribute("lang", "en"),
"XQuery Kick Start"
),
new XElement("author", "Giada De Laurentiis"),
new XElement("author", "Per Bothner"),
new XElement("author", "Kurt Cagle"),
new XElement("author", "James Linn"),
new XElement("author", "Vaidyanathan Nagarajan"),
new XElement("year", "2003"),
new XElement("price", "49.99")
);
//// insert an element to an appropriate location.
xDoc.Element("bookstore").Elements("book").First().AddBeforeSelf(book);
//// save to your disk.
xDoc.Save("bookstore.xml");
//// output to console.
Console.WriteLine(xDoc);
string bookstoreStr = null;
//// read the file which you saved just.
using (System.IO.StreamReader sr = new System.IO.StreamReader("bookstore.xml"))
{
bookstoreStr = sr.ReadToEnd();
}
//// make string to XDocument.
xDoc = XDocument.Parse(bookstoreStr);
//// just a separator.
Console.WriteLine("\n=====================================================");
//// output what your want to see.
var test = xDoc.Elements("bookstore").Elements("book")
.Where(b => Convert.ToDecimal(b.Element("price").Value) > 30)
.Select(p => p);
foreach (var el in test)
{
Console.WriteLine(el.Element("title").Attribute("lang").Value);
}
}
}
}
* Author: sandals
* Date : 2010-08-07 11:24
* Description: Demo for System.Xml.Linq
* */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
namespace Sandals.LinqDemo
{
class Program
{
static void Main(string[] args)
{
//// Creat a new xml document
XDocument xDoc = new XDocument();
xDoc.Declaration = new XDeclaration("1.0", "utf-8", "yes");
XComment xComment = new XComment("This is sandals' test demo");
//// add a new element to XDocument
xDoc.Add(xComment);
XElement bookstore = new XElement("bookstore");
xDoc.Add(bookstore);
XElement book = new XElement("book",
new XAttribute("category", "COOKING"),
new XElement("title",
new XAttribute("lang", "en"),
"Everyday Italian"
),
new XElement("author","Giada De Laurentiis"),
new XElement("year","2005"),
new XElement("price", "30")
);
bookstore.Add(book);
book = new XElement("book",
new XAttribute("category", "CHILDREN"),
new XElement("title",
new XAttribute("lang", "en"),
"Harry Potter"
),
new XElement("author", "J K. Rowling"),
new XElement("year", "2005"),
new XElement("price", "29.99")
);
bookstore.Add(book);
book = new XElement("book",
new XAttribute("category", "WEB"),
new XElement("title",
new XAttribute("lang", "en"),
"XQuery Kick Start"
),
new XElement("author", "Giada De Laurentiis"),
new XElement("author", "Per Bothner"),
new XElement("author", "Kurt Cagle"),
new XElement("author", "James Linn"),
new XElement("author", "Vaidyanathan Nagarajan"),
new XElement("year", "2003"),
new XElement("price", "49.99")
);
//// insert an element to an appropriate location.
xDoc.Element("bookstore").Elements("book").First().AddBeforeSelf(book);
//// save to your disk.
xDoc.Save("bookstore.xml");
//// output to console.
Console.WriteLine(xDoc);
string bookstoreStr = null;
//// read the file which you saved just.
using (System.IO.StreamReader sr = new System.IO.StreamReader("bookstore.xml"))
{
bookstoreStr = sr.ReadToEnd();
}
//// make string to XDocument.
xDoc = XDocument.Parse(bookstoreStr);
//// just a separator.
Console.WriteLine("\n=====================================================");
//// output what your want to see.
var test = xDoc.Elements("bookstore").Elements("book")
.Where(b => Convert.ToDecimal(b.Element("price").Value) > 30)
.Select(p => p);
foreach (var el in test)
{
Console.WriteLine(el.Element("title").Attribute("lang").Value);
}
}
}
}
运行结果如下:

Code下载:/Files/sandals/Program.rar


浙公网安备 33010602011771号