Query and transform XML

一、    LINQ to XML axis method

The chapter xml file depend on the follow file:

  • Elment

  The Element axis method allows us to select a single XML element by name.If no element found with the name provided to the element axis method,Null value will be returned

XElement root = XElement.Load("categorizedBooks.xml");
XElement dotNetCategory = root.Element("categorys");
  • Attribute

  If we want to query the element for the value of the name attribute.We can use the Attribute axis method.Attribute returns the first matching attribute with the provided XName.otherwise,return null value.

  • Elements

  Sometimes,we want to search the elements that they are child of the Current Xelement which is right for other conditions.we can use the Elements method. As the following.

XElement root = XElement.Load("categorizedBooks.xml");
XElement dotNetCategory = root.Element("category");
XElement books = dotNetCategory.Element("Books");
IEnumerable<XElement> bookElements = books.Elements("book");

  It’s important to remember that Elements only searches the elements that are direct children of the XElement that it’s called on.Sometimes rather than needing just the children of the current element,we want to look at all the elements that exist at any level beneath the current element. It’s for these scenarios that the LINQ to XML API provides the Descendants axis method

  • Descendants

  The Descendants axis method works in the same way as the Elements method,but instead of limiting the elements returned to those that are direct children of the current element,Descendants will traverse all the elements underneath the current element

XElement root = XElement.Load("categorizedBooks.xml");
foreach (XElement bookElement in root.Descendants("book"))
{
       Console.WriteLine((string)bookElement);
}

  It’s important to note that the Descendants axis method does not include itself,in the tree of elements that are searched. If you need to include the current element,use the DescendantsAndSelf axis method. Just like the Descendants axis method, the DescendantsAndSelf method returns an IEnumberable of XElement objects. The only difference is that DescendantsAndSelf includes itself within the set of XElement objects that will be returned.

  • Ancestors

  The Ancestors axis method words The Ancestors axis method works exactly like the Descendants method, except instead of searching down the XML tree, it searches up the tree. It offers the same signature and has the same related methods, AncestorsAndSelf and Ancestor-Nodes

XElement root = XElement.Load("categorizedBooks.xml");
XElement dddBook = root.Descendants("book").Where(book => (string)book == "Domain Driven Design").First();
IEnumerable<XElement> ancestors = dddBook.Ancestors("category").Reverse();
ElementAfterSelf
XElement root = XElement.Load("categorizedBooks.xml"); Element dddBook =root.Descendants("book").Where(book => (string)book == "Domain Driven Design").First(); IEnumerable<XElement> beforeSelf = dddBook.ElementsBeforeSelf();
  • NodesAfterSelf
  • ElementBeforeSelf
  • NodesBeforeSelf

二.Standard query operators

  • Projecting with select
XElement root = XElement.Load("categorizedBooks.xml");
    var titles = root.Descendants("Title")
                .Select(titleElement => (string)titleElement);
  • Filtering With Where
var wpfBooks =  from book in root.Descendants("Item")
                let bookAttributes = book.Element("ItemAttributes")
                let title = ((string)bookAttributes.Element("Title"))
                where title.Contains("Windows Presentation Foundation")
                select title;
  • Ordering and grouping
var wpfBooks =  from book in root.Descendants("Item")
                let bookAttributes = book.Element("ItemAttributes")
                let title = ((string)bookAttributes.Element("Title"))
                orderby title
                select title;

var groups =    from book in root.Descendants("Item")
                let bookAttributes = book.Element("ItemAttributes")
                let title = ((string)bookAttributes.Element("Title"))
                let publisher = (string)bookAttributes.Element("Manufacturer")
                orderby publisher, title
                group title by publisher;

foreach (var group in groups)
{
  Console.WriteLine(group.Count() + " book(s) published by " + group.Key)
  foreach (var title in group)
  {
    Console.WriteLine(" - " + title);
  } }

  

 

 

posted @ 2012-05-18 23:12  pstune  阅读(244)  评论(0编辑  收藏  举报