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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:35:09+00:00 2026-06-15T12:35:09+00:00

I am trying to convert an XML data into dictionary. I am having problems

  • 0

I am trying to convert an XML data into dictionary. I am having problems with identical node names. C# .Net 3.5

Sample XML = the problem is I have no control over this. I just need to process it.

<?xml version="1.0" encoding="utf-8"?>
<Root>
  <a1>val1</a1>
  <a2>val2</a2>
  <Parameter>
    <ParameterName>param1</ParameterName>
    <ParameterValue>paramval1</ParameterValue>
  </Parameter>
  <Parameter>
    <ParameterName>param2</ParameterName>
    <ParameterValue>paramval2</ParameterValue>
  </Parameter>
</Root>

My attempt:

XMLStream.Position = 0;
        XElement xmlDetails2 = XElement.Load(new System.IO.StreamReader(XMLStream));
        var x = xmlDetails2.Elements().ToDictionary(
            e => e.Name.LocalName,
            e => e.Elements()
                      .ToDictionary(
                          f => f.Name.LocalName,
                          f => f.Value));

Error I am getting (which makes sense of course):

An item with the same key has already been added.

Expected result ( from example xml ) :

a1 => val1
a2 => val2
param1 => paramval1
param2 => paramval2
...

I created my own based on @L.B suggestion. It’s not the best solution but it works for now.

public void XMLTODictionary(XElement xmlDetails, ref Dictionary<string, string> dic)
{
    foreach (var node in xmlDetails.Elements())
    {
        if (node.Name.LocalName.Equals("parameter", StringComparison.CurrentCultureIgnoreCase))
        {
                dic.Add(node.Element("ParameterName").Value, node.Element("ParameterValue").Value);
        }
        else
        {
            dic.Add(node.Name.LocalName, node.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-06-15T12:35:10+00:00Added an answer on June 15, 2026 at 12:35 pm

    How about using DynamicXml here

    dynamic root = DynamicXml.Load("a.xml");
    
    Console.WriteLine(root.a1);
    foreach (var p in root.Parameter)
    {
        Console.WriteLine("{0}:{1}",p.ParameterName, p.ParameterValue);
    }
    Console.WriteLine(root.Parameter[0].ParameterValue);
    

    EDIT

    A generic approach would be to get a dictionary Dictionary<string,object>
    But there are some problems while converting an xml to dictionary. For example

    <a1>val1</a1>
    

    dict["a1"] would return val1, but what would this xml return

    <a1 name="valAttr"><name>valName</name><a1>
    

    dict["a1"]["name"] ? valAttr or valName?

    And considering your example, the only difference between dict["a1"] and dict["Parameter"] is that
    Parameter exists more than once under the same parent and it should be thought as an array rather than a
    single element.

    DynamicXml tries to solve these issues. Of course there
    is a lot of room for improvement but It should work for basic needs.

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

Sidebar

Related Questions

Can anyone please help. I am trying to convert excel data into xml file
I'm trying to convert an xml document to Python data structures. A sample of
I'm trying to convert my data layer from Linq2Sql to nHibernate. I think Xml
I'm trying to convert some simple .htaccess rewrites into web.config xml; all is well
I'm trying to convert an XML file into the markup used by dokuwiki, using
I have a problem. I have an XML spreadsheet file that I'm trying to
I'm trying to convert some XML data coming from a CLOB to a XMLType
I have this XML document I am trying to read in and insert into
Having difficulty grabbing data from an XML and putting it into an Array. At
I am trying to convert data from database to XML in android. After that

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.