Creating a dictionary from xml



Given this XML:



<dataroot>
<TestTable>
<Tag>tag1</Tag>
<FName>t1</FName>
</TestTable>
<TestTable>
<Tag>tag2</Tag>
<FName>t2</FName>
</TestTable>
<TestTable>
<Tag>tag3</Tag>
<FName>t3</FName>
</TestTable>
</dataroot>


How can I load it into a Dictionary so that if I look up "Tag3", it returns "t3"?


I have tried:



var doc = XDocument.Load("ConfigItems.xml");
Dictionary<string, string> ConfigItems;
ConfigItems = doc.Descendants("TestTable").ToDictionary(v => v.Attribute("Tag").Value.ToString (), v => v.Attribute("FName").Value.ToString());


But I get an "Object reference not set to an instance of an object." error.


No comments:

Post a Comment