)
)
);
}
}
}
The indentation gives you an overall visualization of the document structure.
To save the XML document to file, use the Save()
method:
library.Save("Books.xml");
To print out the XML document as a string, use the ToString()
method:
Console.WriteLine(library.ToString());
When printed, the XML document looks like this:
Wrox
Wrox
O'Reilly
Apress
O'Reilly
To load an XML document into the XDocument object, use the Load()
method:
XDocument LibraryBooks = new XDocument();
LibraryBooks = XDocument.Load("Books.xml");
You can use LINQ to XML to locate specific elements. For example, to retrieve all books published by Wrox, you can use the following query:
var query1 =
from book in LibraryBooks.Descendants("Book")
where book.Element("Publisher").Value == "Wrox"
select book.Element("Title").Value;
Console.WriteLine("------");
Console.WriteLine("Result");
Console.WriteLine("------");
foreach (var book in query1) {
Console.WriteLine(book);
}
This query generates the following output:
------
Result
------
C# 2008 Programmers' Reference
Professional Windows Vista Gadgets Programming
To retrieve all not-yet-published (NYP) books from Wrox, you can use the following query:
var query2 =
from book in library.Descendants("Book")
where book.Attribute("published").Value == "NYP" &&
book.Element("Publisher").Value=="Wrox"
select book.Element("Title").Value;
You can shape the result of a query as you've seen in earlier sections:
var query3 =
from book in library.Descendants("Book")
where book.Element("Publisher").Value == "Wrox"
select new {
Name = book.Element("Title").Value,
Pub = book.Element("Publisher").Value
};
Console.WriteLine("------");
Console.WriteLine("Result");
Console.WriteLine("------");
foreach (var book in query3) {
Console.WriteLine("{0} ({1})", book.Name, book.Pub);
}
This code generates the following output:
------
Result
------
C# 2008 Programmers' Reference (Wrox)
Professional Windows Vista Gadgets Programming (Wrox)
Besides using an anonymous type to reshape the result, you can also pass the result to a non-anonymous type. For example, suppose that you have the following class definition:
public class Book {
public string Name { get; set; }
public string Pub { get; set; }
}
You can shape the result of a query to the Book
class, as the following example shows:
var query4 =
from book in library.Descendants("Book")
where book.Element("Publisher").Value == "Wrox"
select new Book {
Name = book.Element("Title").Value,
Pub = book.Element("Publisher").Value
};
List books = query4.ToList();
Let's now take a look at the usefulness of LINQ to XML. Suppose that you want to build an application that downloads an RSS document, extracts the title of each posting, and displays the link to each post.
Figure 14-13 shows an example of an RSS document.
Figure 14-13
To load an XML document directly from the Internet, you can use the Load() method from the XDocument
class:
XDocument rss =
XDocument.Load(@"http://www.wrox.com/WileyCDA/feed/RSS_WROX_ALLNEW.xml");
To retrieve the title of each posting and then reshape the result, use the following query:
var posts =
from item in rss.Descendants("item")
select new {
Title = item.Element("title").Value,
URL = item.Element("link").Value
};
In particular, you are looking for all the elements and then for each element found you would extract the values of the and
elements.
...
...
...
...
...
Finally, print out the title and URL for each post:
foreach (var post in posts) {
Console.WriteLine("{0}", post.Title);
Console.WriteLine("{0}", post.URL);
Console.WriteLine();
}
Figure 14-14 shows the output.
Figure 14-14
Query Elements with a Namespace
If you observe the RSS document structure carefully, you notice that the element has the dc
namespace defined (see Figure 14-15).
Figure 14-15
The dc
namespace is defined at the top of the document, within the element (see Figure 14-16).
Figure 14-16
Читать дальше