Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / April 2008

Tip: Looking for answers? Try searching our database.

XMl Linq question

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
CSharper - 01 Apr 2008 19:52 GMT
I am trying to find a first element from an xml file using the
following code
XElement t = xml.Descendants("Element").First();

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 - 02 Apr 2008 12:50 GMT
> I am trying to find a first element from an xml file using the
> following code
[quoted text clipped - 4 lines]
> 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").FirstOrDefault();
that way t will be the first 'Element' descendant or nul if there is none.

Signature

    Martin Honnen --- MVP XML
    http://JavaScript.FAQTs.com/

CSharper - 02 Apr 2008 15:15 GMT
> > I am trying to find a first element from an xml file using the
> > following code
[quoted text clipped - 13 lines]
>         Martin Honnen --- MVP XML
>        http://JavaScript.FAQTs.com/

Thanks for the help.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.