Solved

c# turn string with xml into dictionary

Posted on 2014-02-17
2
1,815 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
2 Comments
 
LVL 52

Assisted Solution

by:Carl Tawn
Carl Tawn earned 250 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:
Michael74 earned 250 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now