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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:07:24+00:00 2026-06-16T05:07:24+00:00

I need a little push in the right direction on how to parse an

  • 0

I need a little push in the right direction on how to parse an entire XML document to a dictionary. My plan is to have the key be the path, with each nested type split up by “->”. For instance:

<Foo>
    <Bar>3</Bar>
    <Foo>
        <Bar>10</Bar>
    </Foo>
</Foo>

If I wanted to get a value I’d just grab it out of the dictionary using:

string value = Elements["Foo->Bar"];

I’m not really sure how to go through each element recursively though.
Any help appreciated.

  • 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-16T05:07:25+00:00Added an answer on June 16, 2026 at 5:07 am

    Straightforward solution:

        private static string GetElementPath(XElement element)
        {
            var parent = element.Parent;
            if(parent == null)
            {
                return element.Name.LocalName;
            }
            else
            {
                return GetElementPath(parent) + "->" + element.Name.LocalName;
            }
        }
    
        static void Main(string[] args)
        {
            var xml = @"
                <Foo>
                    <Bar>3</Bar>
                    <Foo>
                        <Bar>10</Bar>
                    </Foo>
                </Foo>";
            var xdoc = XDocument.Parse(xml);
            var dictionary = new Dictionary<string, string>();
            foreach(var element in xdoc.Descendants())
            {
                if(!element.HasElements)
                {
                    var key = GetElementPath(element);
                    dictionary[key] = (string)element;
                }
            }
            Console.WriteLine(dictionary["Foo->Bar"]);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a little push in the right direction. Here's my problem: I have
I've just started a project and need a little push in the right direction.
I almost have this solved but need a little push. Here's what I have:
So I am in need of a little nudge or push in the right
Need a little help with a SQL / ActiveRecord query. Let's say I have
I need a little piece of advice. I have a test page with 2
I am using the gmap3 jquery plugin and need a little help. I have
I've almost got this but just need a little push over the edge. I'm
I need a little help on a problem with navigation controllers. I have a
I need little help for url rewriting in wordpress. basically what i need: mydomain.com/mysomewordpresspost

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.