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

  • Home
  • SEARCH
  • 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

Ask A Question

Stats

  • Questions 64k
  • Answers 64k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer You need to specify the line height to match the… May 11, 2026 at 10:54 am
  • added an answer I decided to write my own drag and drop list… May 11, 2026 at 10:54 am
  • added an answer You can't just draw at any time like that. You… May 11, 2026 at 10:54 am

Related Questions

Can someone explain the mechanics of a jump table and why is would be
Can someone explain the main benefits of different types of references in C#? Weak
Can someone explain the use of DBCC DROPCLEANBUFFERS together with the CHECKPOINT operator and
Can someone explain the syntax in the source attribute here? What the heck is
Can someone explain me the reason of overflow in variable a? Note that b
Can someone help explain the following: If I type: a=`ls -l` Then the output
In WPF: Can someone please explain the relationship between DependencyProperty and Databinding? I have
Can someone explain to me the advantages of using an IOC container over simply
Can someone explain what exactly the string 0 but true means in Perl? As
Can someone explain what are the benefits of using the @import syntax comparing to

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.