1

I want to parse an xml file into a dictionary the Dictionary looks like this

"250", 0.110050251256281
"150", 0.810050256425628
"850", 0.701005025125628
"550", 0.910050251256281

How can i parse the data above into a dictionary from the xml file below

<?xml version="1.0" encoding="utf-8"?>
<calibration>
  <zoom level="250">0,110050251256281</zoom>
  <zoom level="150">0,810050256425628</zoom>
  <zoom level="850">0,701005025125628</zoom>
  <zoom level="550">0,910050251256281</zoom>
</calibration>

any help would be much appreciated

3
  • 1
    Use a StringReader and Split to break up the CSV file and then use an XDocument to create the XML. Plenty of information on MSDN. Commented Apr 11, 2013 at 11:19
  • What code do you have already? If you show it, we can try to fix it. Commented Apr 11, 2013 at 11:20
  • The question is... What have you tried? Commented Apr 11, 2013 at 11:20

5 Answers 5

4

You can use System.Xml.Linq.XDocument:

System.Xml.Linq.XDocument doc = System.Xml.Linq.XDocument.Load("your file");
var nodes = doc.Element("calibration").Elements("zoom");
Dictionary<string, double> myDictionary = new Dictionary<string, double>();

foreach (System.Xml.Linq.XElement item in nodes)
{
    var level = item.Attribute("level").Value;
    var val = double.Parse(item.Value);
    myDictionary.Add(level, var);
}
Sign up to request clarification or add additional context in comments.

Comments

4

I would do it like this:

  var xml = @"<?xml version="1.0" encoding="utf-8"?>
              <calibration>
                  <zoom level="250">0,110050251256281</zoom>
                  <zoom level="150">0,810050256425628</zoom>
                  <zoom level="850">0,701005025125628</zoom>
                  <zoom level="550">0,910050251256281</zoom>
              </calibration>"

  var doc = XDocument.Parse(xml);
  var zooms = doc.Descendants("zoom")
                 .ToDictionary(x => x.Attribute("level").Value, x => x.Value)

Comments

1

Try like below... it will help you...

Code :

 System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
 doc.Load(Environment.CurrentDirectory + "//XML//Sample.xml");
 System.Xml.XmlNodeList CNodes = doc.SelectNodes("/calibration/zoom");
 Dictionary<int, string> dictionary = new Dictionary<int, string>();
 foreach (System.Xml.XmlNode node in CNodes)
   dictionary.Add(Convert.ToInt32(node.Attributes["level"].Value), node.InnerText);

Output :

enter image description here

1 Comment

op has asked for linq-xml solution..This doesn't ans his question!..also linq-xml is better then that old low-level api
1

Using Linq:

XDocument doc = XDocument.Load("XmlFile");
var elements = (from items in doc.Elements("calibration").Elements("zoom")
                select items).ToDictionary(x => x.Attribute("level").Value, x => Convert.ToDouble(x.Value));

Comments

0

Something like this should work. Don't forget to add some errorhandling and you may want to load the xml from a file instead of a hardcoded string.

string xml = @"<?xml version=""1.0"" encoding=""utf-8""?>
<calibration>
<zoom level=""250"">0,110050251256281</zoom>
<zoom level=""150"">0,810050256425628</zoom>
<zoom level=""850"">0,701005025125628</zoom>
<zoom level=""550"">0,910050251256281</zoom>
</calibration>";

System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
xmlDoc.LoadXml(xml);
var nodes = xmlDoc.SelectNodes("calibration/zoom");
var dicNodes = new Dictionary<string,string>();
foreach (System.Xml.XmlNode node in nodes)
{
    dicNodes.Add(node.Attributes["level"].Value, node.InnerText);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.