?
Solved

c# turn string with xml into dictionary

Posted on 2014-02-17
2
Medium Priority
?
2,159 Views
Last Modified: 2014-02-19
I have the string below as input.  How can I get a dictionary object out of it?  The parameters might change, which is why I want a dictionary.

var stringXml = "<width>100</width><height>200</height><controller>MyConttoller</controller>";

I have seen people mention ToDictionary but I could not figure out how to implement it based on this input.
0
Comment
Question by:jackjohnson44
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 52

Assisted Solution

by:Carl Tawn
Carl Tawn earned 1000 total points
ID: 39865807
If you want to treat it as XML then you'll need to wrap a root node around it all. Then you can do something like:
var stringXml = "<settings><width>100</width><height>200</height><controller>MyConttoller</controller></settings>";
            
XDocument doc = XDocument.Parse(stringXml);
var dict = doc.Element("settings").Elements().Select(t => new { t.Name, t.Value }).ToDictionary(t => t.Name, t => t.Value.ToString());

Open in new window

0
 
LVL 23

Accepted Solution

by:
Michael Fowler earned 1000 total points
ID: 39865853
For this solution your xml needs a root node which I have made as root and you also need the namespaces System.Xml, & System.Collections.Generic

         XmlDocument xmlDoc = new XmlDocument();
         xmlDoc.LoadXml(@"<root><width>100</width><height>200</height><controller>MyConttoller</controller></root>");
         Dictionary<string,string> dict = new Dictionary<string,string>();
         foreach (XmlElement node in xmlDoc.DocumentElement)
         {
            dict.Add(node.Name, node.InnerText);
         }

Open in new window

0

Featured Post

Setting up LaraDock for Laravel

Learn how to set up LaraDock in a Laravel project - LaraDock gives us an easy way to run a Laravel application using Docker in a single command.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question