XMl Linq question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • CSharper

    XMl Linq question

    I am trying to find a first element from an xml file using the
    following code
    XElement t = xml.Descendants ("Element").Fir st();

    This works fine, but when there is no result set ofcourse the first
    method call bombs. I can always split the statement into two and check
    if there are any elements available before I pick the first element.
    Is it possible to do it in a single statement?

    something like the following

    from e1 in xml.Decendants( "Element")
    select top 1;

    Thanks.
  • Martin Honnen

    #2
    Re: XMl Linq question

    CSharper wrote:
    I am trying to find a first element from an xml file using the
    following code
    XElement t = xml.Descendants ("Element").Fir st();
    >
    This works fine, but when there is no result set ofcourse the first
    method call bombs. I can always split the statement into two and check
    if there are any elements available before I pick the first element.
    Is it possible to do it in a single statement?
    Use
    XElement t = xml.Descendants ("Element").Fir stOrDefault();
    that way t will be the first 'Element' descendant or nul if there is none.

    --

    Martin Honnen --- MVP XML

    Comment

    • CSharper

      #3
      Re: XMl Linq question

      On Apr 2, 6:50 am, Martin Honnen <mahotr...@yaho o.dewrote:
      CSharper wrote:
      I am trying to find a first element from an xml file using the
      following code
      XElement t = xml.Descendants ("Element").Fir st();
      >
      This works fine, but when there is no result set ofcourse the first
      method call bombs. I can always split the statement into two and check
      if there are any elements available before I pick the first element.
      Is it possible to do it in a single statement?
      >
      Use
         XElement t = xml.Descendants ("Element").Fir stOrDefault();
      that way t will be the first 'Element' descendant or nul if there is none.
      >
      --
      >
              Martin Honnen --- MVP XML
             http://JavaScript.FAQTs.com/
      Thanks for the help.

      Comment

      Working...