嵌套XML到字典 [英] Nested XML to Dictionary
问题描述
我正在尝试将XML数据转换为字典。我在使用相同的节点名称时遇到问题。 C#.Net 3.5
示例XML =问题是我对此无能为力。我只需要对其进行处理。
<?xml version = 1.0 encoding = utf-8?>
< Root>
< a1&val;< / a1>
< a2" val2< / a2>
< Parameter>
< ParameterName> param1< / ParameterName>
< ParameterValue> paramval1< / ParameterValue>
< / Parameter>
< Parameter>
< ParameterName> param2< / ParameterName>
< ParameterValue> paramval2< / ParameterValue>
< / Parameter>
< / Root>
我的尝试:
XMLStream.Position = 0;
XElement xmlDetails2 = XElement.Load(新System.IO.StreamReader(XMLStream));
var x = xmlDetails2.Elements()。ToDictionary(
e => e.Name.LocalName,
e => e.Elements()
.ToDictionary(
f => f.Name.LocalName,
f => f.Value));
我遇到的错误(这当然很有意义):
已添加具有相同密钥的项目。
预期结果(来自示例xml):
< pre class = lang-none prettyprint-override>
a1 => val1
a2 => val2
param1 => paramval1
param2 => paramval2
...
我是根据@ L.B建议创建的。
public void XMLTODictionary(XElement xmlDetails,ref字典< string,string> dic)
{
foreach(xmlDetails.Elements()中的var node)
{
if(node.Name.LocalName.Equals( parameter ,StringComparison.CurrentCultureIgnoreCase))
{
dic.Add(node.Element( ParameterName)。Value,node.Element( ParameterValue)。Value);
}
else
{
dic.Add(node.Name.LocalName,node.Value);
}
}
}
如何在此处使用 DynamicXml
动态根= DynamicXml.Load( a.xml);
Console.WriteLine(root.a1);
foreach(root参数中的var p)
{
Console.WriteLine( {0}:{1},p.ParameterName,p.ParameterValue);
}
Console.WriteLine(root.Parameter [0] .ParameterValue);
编辑
一种通用方法是获取字典 Dictionary< string,object>
,但是将xml转换为字典时会出现一些问题。例如
< a1> val1< / a1>
dict [ a1]
将返回 val1 ,但是此xml会返回什么
< a1 name = valAttr>< ; name> valName< / name>< a1>
dict [ a1] [ name]
? valAttr 或 valName ?
并考虑您的示例, dict之间的唯一区别[ a1]
和 dict [ Parameter]
是
Parameter 在以下情况下存在多次同一父级,应该将其视为数组而不是
单个元素。
DynamicXml 试图解决这些问题。当然,
有很大的改进空间,但是应该可以满足基本需求。
I am trying to convert an XML data into dictionary. I am having problems with identical node names. C# .Net 3.5
Sample XML = the problem is I have no control over this. I just need to process it.
<?xml version="1.0" encoding="utf-8"?>
<Root>
<a1>val1</a1>
<a2>val2</a2>
<Parameter>
<ParameterName>param1</ParameterName>
<ParameterValue>paramval1</ParameterValue>
</Parameter>
<Parameter>
<ParameterName>param2</ParameterName>
<ParameterValue>paramval2</ParameterValue>
</Parameter>
</Root>
My attempt:
XMLStream.Position = 0;
XElement xmlDetails2 = XElement.Load(new System.IO.StreamReader(XMLStream));
var x = xmlDetails2.Elements().ToDictionary(
e => e.Name.LocalName,
e => e.Elements()
.ToDictionary(
f => f.Name.LocalName,
f => f.Value));
Error I am getting (which makes sense of course):
An item with the same key has already been added.
Expected result ( from example xml ) :
a1 => val1
a2 => val2
param1 => paramval1
param2 => paramval2
...
I created my own based on @L.B suggestion. It's not the best solution but it works for now.
public void XMLTODictionary(XElement xmlDetails, ref Dictionary<string, string> dic)
{
foreach (var node in xmlDetails.Elements())
{
if (node.Name.LocalName.Equals("parameter", StringComparison.CurrentCultureIgnoreCase))
{
dic.Add(node.Element("ParameterName").Value, node.Element("ParameterValue").Value);
}
else
{
dic.Add(node.Name.LocalName, node.Value);
}
}
}
How about using DynamicXml here
dynamic root = DynamicXml.Load("a.xml");
Console.WriteLine(root.a1);
foreach (var p in root.Parameter)
{
Console.WriteLine("{0}:{1}",p.ParameterName, p.ParameterValue);
}
Console.WriteLine(root.Parameter[0].ParameterValue);
EDIT
A generic approach would be to get a dictionary Dictionary<string,object>
But there are some problems while converting an xml to dictionary. For example
<a1>val1</a1>
dict["a1"]
would return val1, but what would this xml return
<a1 name="valAttr"><name>valName</name><a1>
dict["a1"]["name"]
? valAttr or valName?
And considering your example, the only difference between dict["a1"]
and dict["Parameter"]
is that
Parameter exists more than once under the same parent and it should be thought as an array rather than a
single element.
DynamicXml tries to solve these issues. Of course there is a lot of room for improvement but It should work for basic needs.
这篇关于嵌套XML到字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!