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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:20:32+00:00 2026-05-23T16:20:32+00:00

I have xml file <root> <child_1> <sub_child attr1=text attr2=text /> <sub_child attr1=text attr2=text />

  • 0

I have xml file

<root>
  <child_1>
    <sub_child attr1="text" attr2="text" />
    <sub_child attr1="text" attr2="text" />
    <sub_child attr1="text" attr2="text" />
  </child_1>    
  <child_2>
    <sub_child attr1="text" attr2="text" />
    <sub_child attr1="text" attr2="text" />
    <sub_child attr1="text" attr2="text" />
  </child_2>    
  <child_3>
    <sub_child_1 attr1="text" attr2="text" />
    <sub_child_2 attr1="text" attr2="text" />
  </child_3>    
  ...

and I want to read sub_child attributes to classes

class SubChild {
  string a1;
  string a2;
}

I need two lists of SubChild objects for child_1 and child_2 and two SubChild objects for sub_child_1 and sub_child_2

Now I use linq to xml and my code is

// List<SubChild> 
var child_1 = from s in doc.Descendants("sub_child").Where(x => x.Parent.Name == "child_1")
              select new SubChild {
                a1 = s.Attribute("attr1").Value,
                a2 = s.Attribute("attr2").Value,
              };

// List<SubChild> 
var child_2 = from s in doc.Descendants("sub_child").Where(x => x.Parent.Name == "child_2")
              select new SubChild {
                a1 = s.Attribute("attr1").Value,
                a2 = s.Attribute("attr2").Value,
              };

// SubChild
var sub_1 = (from s in doc.Descendants("sub_child_1")
              select new SubChild {
                a1 = s.Attribute("attr1").Value,
                a2 = s.Attribute("attr2").Value,
              }).First();

// SubChild
var sub_2 = (from s in doc.Descendants("sub_child_2")
              select new SubChild {
                a1 = s.Attribute("attr1").Value,
                a2 = s.Attribute("attr2").Value,
              }).First();

but it looks ugly and I would like to ask is there more clear way to do this?

  • 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-05-23T16:20:33+00:00Added an answer on May 23, 2026 at 4:20 pm

    You can specify a “Path” using .Element() and .Elements(), eliminating the where lambdas.

     // untested
     var child_1 = from s in doc.Root.Element("child_1").Elements("sub_child")
              select new SubChild {
                a1 = s.Attribute("attr1").Value,
                a2 = s.Attribute("attr2").Value,
              };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have an empty XML file like so: <root></root> And I want
I have an xml file like this: <root> <item> <name>one</name> <status>good</status> </item> <item> <name>two</name>
I have an XML file that looks like <?xml version='1.0' encoding='UTF-8'?> <root> <node name=foo1
Say I have this given XML file: <root> <node>x</node> <node>y</node> <node>a</node> </root> And I
We have an XML file with a very simple implementation of XLink : <root
I have a XML file, like this: <?xml version=1.0 encoding=utf-8 ?> <ROOT> <NAME> ItemName
I Have this XML File <?xml version=1.0 standalone=yes?> <Root> <Object> <referenceName>People</referenceName> <query>select * from
I have been trying to read an xml file. I have to extract value
I have XML file , I want to copy it as it is ,
I have an XML file structured as: <root> <featured> <title></title> <tweet></tweet> <img></img> </featured> </root>

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.