2013-04-11 91 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));