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 8029199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T00:21:52+00:00 2026-06-05T00:21:52+00:00

Please review below code and advice what needs to be done in order to

  • 0

Please review below code and advice what needs to be done in order to get the additional last line in output as shown in expected output

class test {

   static void Main(string[] args)
    {

        XDocument doc = XDocument.Load("E:\\BI_analytics\\Data\\so.xml");
        var query = from test in doc.Descendants("tester")
                    from testreq in test.Descendants("testRequest")
                    from testresp in test.Descendants("testResponse")
                    let id = testreq.Element("id") == null ? string.Empty : testreq.Element("id").Value

                  //  select id;
                    from itm in testresp.Descendants("item")
                    select new
                    {
                        ID = (string)id,
                        Name = (string)itm.Attribute("itemname"),
                        Code = (string)itm.Attribute("itemocde"),
                    };


        foreach (var result in query)
        {
            Console.WriteLine(result);
        }
    }
}

Current output

{ ID = 2, Name = test item1, Code = 111 }
{ ID = 2, Name = test item2, Code = 222 }
{ ID = 3, Name = test item3, Code = 333 }
{ ID = 3, Name = test item4, Code = 444 }

Expected output

{ ID = 2, Name = test item1, Code = 111 }
{ ID = 2, Name = test item2, Code = 222 }
{ ID = 3, Name = test item3, Code = 333 }
{ ID = 3, Name = test item4, Code = 444 }
{ ID = 4, Name = , Code = }
<?xml version="1.0" encoding="utf-8"?>
<root>
  <tester>
    <testRequest>
      <id>2</id>
    </testRequest>
    <testResponse>
      <items>
        <item itemname="test item1" itemocde="111"/>
        <item itemname="test item2" itemocde="222"/>
      </items>
    </testResponse>
  </tester>
  <tester>
    <testRequest>
      <id>3</id>
    </testRequest>
    <testResponse>
      <items>
        <item itemname="test item3" itemocde="333"/>
        <item itemname="test item4" itemocde="444"/>
      </items>
    </testResponse>
  </tester>
  <tester>
    <testRequest>
      <id>4</id>
    </testRequest>
    <testResponse>
      <items />
    </testResponse>
  </tester>
</root>
  • 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. Editorial Team
    Editorial Team
    2026-06-05T00:21:54+00:00Added an answer on June 5, 2026 at 12:21 am

    This is the problem:

    from itm in testresp.Descendants("item")
    

    You don’t have any item elements, so you probably want:

    from itm in testresp.Descendants("item").DefaultIfEmpty()
    

    … at which point you need:

    select new
    {
        ID = (string)id,
        Name = itm == null ? "" : (string)itm.Attribute("itemname"),
        Code = itm == null ? "" : (string)itm.Attribute("itemocde"),
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a memory leak at NSDate . Please review the code below. -(void)myMethods:(NSDate
Please review my brief example code below and tell me if there is better,
Please review my .htaccess code below. The code below was suggested by I User
Please review the example code below, I have a class file that is loaded
Please find the below code , when i am rotating my simulator to landscape
Please see below code,i getting wrong value. eg; var FirstValue=0.00; var secondvalue=parseFloat(22.88,10).toFixed(2); var thirdvalue=(FirstValue)
I got another problem with the FCKEditor within ASP.NET MVC. Please review the code
I have handling the exception in my code as below, but still get error
How can line 25 in the code below generate the error that follows? I'm
please give me some advice. I have to send email using SQL database mail.

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.