none
Open Xml not very LINQ friendly? RRS feed

  • Question

  • Please correct me if I am wrong but it appears that most of the methods in the SDK with regard to object retrieval return single objects.  For example there is a GetFirstChild but where is GetChildren, there is a NextSibling but where is NextSiblings?  I am very familiar with LINQ to XML and LINQ to Objects but this is nothing like it.

    Please, help me get LINQ back into my Open XML code by proving me wrong.


    Thanks, -ja

    Tuesday, May 7, 2013 4:30 PM

Answers

  • If you want to work with the child elements, the ChildElements property will return them.  Also, the Descendants() and Descendants<T>() methods can return all descendant elements or descendants of a specific type.  All are LINQ friendly as the return values can be used with the common LINQ methods such as First(), Single(), Where(), Skip(), etc.

    I don't think there's an equivalent NextSiblings functionality.  I've never needed it but it could probably be created as a simple extension method using ChildElements to get the children, skipping those up to and including the given element, and returning the rest as an enumerable list.

    Eric

    Tuesday, May 7, 2013 7:30 PM

All replies

  • If you want to work with the child elements, the ChildElements property will return them.  Also, the Descendants() and Descendants<T>() methods can return all descendant elements or descendants of a specific type.  All are LINQ friendly as the return values can be used with the common LINQ methods such as First(), Single(), Where(), Skip(), etc.

    I don't think there's an equivalent NextSiblings functionality.  I've never needed it but it could probably be created as a simple extension method using ChildElements to get the children, skipping those up to and including the given element, and returning the rest as an enumerable list.

    Eric

    Tuesday, May 7, 2013 7:30 PM
  • Thanks, my code has gone form a couple of do whiles to an easy to maintain foreach.


    Thanks, -ja

    Tuesday, May 7, 2013 8:52 PM