Solved

c# turn string with xml into dictionary

Posted on 2014-02-17
2
2,085 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 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:
Michael Fowler 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

688 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