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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:19:15+00:00 2026-05-23T00:19:15+00:00

I am preferring LINQ to XML over XMLReader because it feels much easier to

  • 0

I am preferring LINQ to XML over XMLReader because it feels much easier to use. However, I know I’m doing it wrong somewhere. I’m not looking for faster execution or anything, just a cleaner rewrite. I can’t tell if it’ll fit cleanly into the from foo in bar where foo is some condition form, but there’s got to be a cleaner way.

I am not using anonymous objects here and instead outputting as strings because I am excluding some code which will adapt the data into existing objects – however, it is kind of a messy structure currently which I need to refactor.

My XML looks like this.

<?xml version="1.0" encoding="utf-8" ?>
<entity name="Something">
  <Component type="RenderComponent">
    <Material type="string">fur</Material>
    <Reflectiveness type="int">678</Reflectiveness>
  </Component>

  <Component type="HealthComponent">
    <Sound type="int">60</Sound>
  </Component>
</entity>

And my code:

static void Main(string[] args)
{
    XDocument xdoc = XDocument.Load(@"..\..\XMLFile1.xml");
    List<string> comNames = new List<string>();
    Dictionary<string, string> paramValues = new Dictionary<string, string>();
    List<string> paramTypes = new List<string>();

    foreach (XElement e in xdoc.Root.Elements("Component"))
    {
        string type = e.Attribute("type").Value;
        foreach(XElement param in e.Elements())
        {
            paramValues.Add(param.Name.LocalName, param.Value);
            paramTypes.Add(param.Attributes().First().Value);
        }
        Console.WriteLine(" \n\n\n");
        comNames.Add(type);
    }

}

Giving outputs of:
comNames - RenderComponent, HealthComponent
paramValues - (Material, fur), (Reflectiveness, 678), (Sound, 60)
paramTypes - string, int, int

If it clears it up somewhat, the general layout of the file:

  • Root node entity with a name attribute (forgot in this example)
  • n Component nodes with type attributes
  • Each Component node has a number of child nodes which have names, a type attribute, and a value.
  • 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-23T00:19:16+00:00Added an answer on May 23, 2026 at 12:19 am

    I think, you can do something like this:

    XDocument xdoc = XDocument.Load(@"..\..\XMLFile1.xml");
    var data = 
    xdoc.Root.Elements("Component")
             .SelectMany(e => new
                              {
                                type = e.Attribute("type").Value, 
                                paramValues = e.Elements()
                                               .Select(x => new KeyValuePair<string,
                                                                             string>
                                                                  (x.Name.LocalName, 
                                                                   x.Value)),
                                paramType = e.Elements()
                                             .Select(x => x.Attributes().First()
                                                                         .Value)
                              });
    
    List<string> comNames = data.Select(x => x.type).ToList();
    List<string> paramTypes = data.Select(x => x.paramType).ToList();
    Dictionary<string, string> paramValues = data.Select(x => x.paramValues)
                                                 .ToDictionary(x => x.Key, 
                                                               x => x.Value);
    

    But honestly, I think your original code is just fine.

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

Sidebar

Related Questions

This error is referring to sstax= grosspay*SS_TAX. I don't know what I'm doing wrong.
Linq-to-Xml contains lots of methods that allow you to add arbitrary objects to an
I am preferring to manually migrate my tables in Django. Because using automated tools
I have an xml file that I am using linq-to-XML to read. Linq-to-XML is
I am currently reading Pro LINQ c# 2008, and in page 87 the guy
I am working on a winforms application using LINQ to SQL - and am
I'm currently brushing up on LINQ and am trying to comprehend the difference between
I'm just playing with some linq and asp.net - absolute beginner so I'm not
I am trying to convert the following SQL into a LINQ expression SELECT COUNT(ID)
First, I am NOT trying to spur yet another debate about LINQ vs. stored

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.