我有一个如下所示的 XML 文件:
<Cities>
<Name>Seattle</Name>
<State>WA</State>
<Population>552105</Population>
</Cities>
我想将城市信息加载到字典中,以便我的字典看起来像:
cityDictionary("Name") = "Seattle"
cityDictionary("State") = "WA"
cityDictionary("Population") = "552105"
以下代码确实有效:
var doc = XDocument.Load(@"..\..\Cities.xml");
var rootNodes = doc.Root.DescendantNodes().OfType<XElement>();
var keyValuePairs = from n in rootNodes
select new
{
TagName = n.Name,
TagValue = n.Value
};
Dicitionary<string, string> allItems = new Dictionary<string, string>();
foreach (var token in keyValuePairs) {
allItems.Add(token.TagName.ToString(), token.TagValue.ToString());
}
但我想做这一步。
有什么建议么?