zoukankan      html  css  js  c++  java
  • 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);
      } }

      

  • 相关阅读:
    关于CSDN指针讨论的心得
    VC++ 6.0 与VS2008 C++ DEBUG工具(Windows)介绍
    VC++ 申明静态变量的注意事项
    大家好,我是新的blue1000~
    [讨论]当我采用动态sql绑定datagrid分页的时候,遇到的问题
    我与Google有个对话
    [BK专访]一切以客户为中心,其它一切纷至沓来
    [译]在.net中使用GDI+来提高gif图片的保存画质
    微软能不能别这样坑爹啊,即使不中毒,也伤不起啊
    长见识!1021字节javascript写成的3D圣诞树
  • 原文地址:https://www.cnblogs.com/PerfectSoft/p/2508432.html
Copyright © 2011-2022 走看看