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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:50:16+00:00 2026-05-20T07:50:16+00:00

I have a string containing field/value pairs in xml format and want to parse

  • 0

I have a string containing field/value pairs in xml format and want to parse it into Dictionary object.

string param = "<fieldvaluepairs>
<fieldvaluepair><field>name</field><value>books</value></fieldvaluepair>
<fieldvaluepair><field>value</field><value>101 Ways to Love</value></fieldvaluepair>
<fieldvaluepair><field>type</field><value>System.String</value></fieldvaluepair>
</fieldvaluepairs>";

Is there a quick and simple way to read and store all the field/value pairs in Dictionary object? No LINQ please.

Also, there could be possibility that the value field can contain an xml string itself. the solutions provided is wonderful but fails if the value is xml iteself. Example: If the value is something like

<?xml version="1.0" encoding="utf-8"?><GetCashFlow xmlns="MyServices"><inputparam><ac_unq_id>123123110</ac_unq_id></inputparam></GetCashFlow>

It errors out with the message:

Unexpected XML declaration. The XML
declaration must be the first node in
the document, and no white space
characters are allowed to appear
before it.

Please feel free to suggest any modification in xml format if you think it can make things easier to store in Dictionary object.

  • 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-20T07:50:16+00:00Added an answer on May 20, 2026 at 7:50 am
    using System;
    using System.Linq;
    using System.Xml.Linq;
    using System.Xml;
    using System.Collections.Generic;
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                var xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><r><a><k>key1</k><v><![CDATA[<?xml version=\"1.0\" encoding=\"utf-8\"?><foo><bar>Hahaha</bar></foo>]]></v></a><a><k>key2</k><v>value2</v></a></r>";
    
                 PrintDictionary(XmlToDictionaryLINQ(xml));
                 PrintDictionary(XMLToDictionaryNoLINQ(xml));
            }
    
            private static Dictionary<string, string> XMLToDictionaryNoLINQ(string xml)
            {
                var doc = new XmlDocument();
                doc.LoadXml(xml);
    
                var nodes = doc.SelectNodes("//a");
    
                var result = new Dictionary<string, string>();
                foreach (XmlNode node in nodes)
                {
                    result.Add(node["k"].InnerText, node["v"].InnerText);
                }
    
                return result;
            }
    
            private static Dictionary<string, string> XmlToDictionaryLINQ(string xml)
            {
                var doc = XDocument.Parse(xml);
                var result =
                    (from node in doc.Descendants("a")
                     select new { key = node.Element("k").Value, value = node.Element("v").Value })
                    .ToDictionary(e => e.key, e => e.value);
                return result;
            }
    
            private static void PrintDictionary(Dictionary<string, string> result)
            {
                foreach (var i in result)
                {
                    Console.WriteLine("key: {0}, value: {1}", i.Key, i.Value);
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Right now I have a dictionary containing a string value and an NSTextField in
I have a string containing a date, and another string containing the date format
I have a column of data containing a string with the following format: blablablabla(XYZ)
I have a string containing byte data. How can I perform an in-place conversion
In a Python package, I have a string containing (presumably) the name of a
If I have an f# function that takes a string containing a sentence, what
I have an Excel spreadsheet containing a list of strings. Each string is made
In C++, I have a map<string, string> , containing an unknown number of entries.
I have string like this /c SomeText\MoreText Some Text\More Text\Lol SomeText I want to
I have got a task table in my database with a priority field containing

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.