2013-04-11 90 views
1

我想解析XML文件转换成字典中的解释是这样的如何在c#中解析这个xml?

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

如何可以解析上述数据转换成字典从下面

<?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> 

任何帮助的xml文件会非常感谢

+1

用一个StringReader和斯普利特,打破了CSV文件,然后使用一个XDocument创建XML。有关MSDN的大量信息。 – tomasmcguinness 2013-04-11 11:19:57

+0

你有什么代码?如果你展示它,我们可以尝试修复它。 – 2013-04-11 11:20:28

+0

问题是... [你有什么尝试?](http://whathaveyoutried.com/) – Carsten 2013-04-11 11:20:45

回答

4

您可以使用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); 
} 
4

我会做这样的:

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) 
1

尝试像下面......它会帮助你...

代码:

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); 

输出:

enter image description here

+0

op已经要求'linq-xml'解决方案..这不回答他的问题!..也linq- xml比那个**旧**低级别的api更好 – Anirudha 2013-04-11 11:34:26

0

像这样的东西应该可以工作。不要忘记添加一些错误处理,你可能想从文件中加载xml而不是硬编码的字符串。

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); 
} 
1

使用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));