Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 152231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T09:35:11+00:00 2026-05-11T09:35:11+00:00

Can someone explain the LINQ to XML below? Also, what is the correct way

  • 0

Can someone explain the LINQ to XML below? Also, what is the correct way to check if the method returned a List with Data? Do you just check if the List is empty.

Code:

public List<Listing> GetList() {     if (File.Exists(this.xmlFilePath))     {         XDocument doc = XDocument.Load(this.xmlFilePath);          var listings = from row in doc.Root.Elements('listing')                        select new Listing                        {                            A = (string)row.Element('A'),                            B = (string)row.Element('B'),                            C = (string)row.Element('C'),                            D = (string)row.Element('D'),                            E = (string)row.Element('E')                        };          return listings.ToList();     }     else     {         return new List<Listing>();     } } 

XML:

<Listings>    <listing>      <a>A</a>      <b>B</b>      <c>C</c>      <d>D</d>      <e>E</e>     </listing>     <listing>      <a>F</a>      <b>G</b>      <c>C</c>      <d>H</d>      <e>I</e>     </listing> </Listings> 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T09:35:11+00:00Added an answer on May 11, 2026 at 9:35 am

    You can check if listings has any data by examining the result of the Count() method on the listings variable, and to answer the question asked in the comments section below, ‘So, there is no way to actually return null? I have to return a List<listing>() object?’, please see the following code:

    public List<Listing> GetList() {   if (File.Exists(this.xmlFilePath))   {     XDocument doc = XDocument.Load(this.xmlFilePath);      var listings = from row in doc.Root.Elements('listing')              select new Listing              {                A = (string)row.Element('A'),                B = (string)row.Element('B'),                C = (string)row.Element('C'),                D = (string)row.Element('D'),                E = (string)row.Element('E')              };      // Check if we have any matches              if(listings.Count() > 0)     {       return listings.ToList();     }      return null;   }   return null; } 

    I think that’s what you’re after.

    There isn’t much to explain about how the code works. In the first part of the query:

    var listings = from row in doc.Root.Elements('listing') 

    The expression doc.Root.Elements('listing') selects all of the <listing> elements below the document root (<Listings>).

    The second part of the query creates a new Listing object for each <listing> element and assigns the inner text of each of the A,B,C,D and E child elements to each of the A,B,C,D and E properties of each new Listing object created.

    The line containing: return listings.ToList(); returns a generic List<T> of Listing objects.

    Using the sample XML provided, you’ll find that each Listing object’s A,B,C,D, and E properties will be null. This is because the character case of each of the row.Element() selectors is different from the XML being queried, i.e. your XML has <a> and row.Element('A') uses an uppercase 'A'. XML node matching in LINQ to XML is case-sensitive.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

can someone explain me how to check LINQ generated SQL queries in VWD 2008?
Can someone explain how the LINQ functions Where(..) and FindAll(..) differ? They both seem
Can someone please explain the below to me. First is how I call the
Can someone explain the question mark in the following code? Also INITIAL_PERMANCE is a
Can someone explain why in the below example the column name for why appears
I'm having trouble to convert this SQL to Linq To Entities. Can someone explain
Can someone explain to me why my EF (4.3) code first code below is
Can someone explain why this exception is occuring the in the following LINQ query:
Can someone explain this simple, yet deceiving, anomaly? There are two models, where B
Can someone explain me why this works: select val1, val2, count(case when table2.someID in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.