[英]Convert xml contents to a key value pair in C#
Is there any way to convert an xml to key-value pair in an easier way without iterating through every nodes and elements 有没有办法以更简单的方式将xml转换为键值对,而无需遍历每个节点和元素
My XML structure is similar to this 我的XML结构与此类似
<root>
<StandardValues>
<ButtonYES alias="customname value">Ja</ButtonYES>
<ButtonNO>Nei</ButtonNO>
</StandardValues>
<Page1>
<Key_Head>2011 Applications</Key_Head>
<Key_Title>Title from 2011</Key_Title>
<Key_Param1>Parameter value</Key_Param1>
</Page1>
<Page2>
<Page_Head>2011 Applications</Page_Head>
<page_Title>Title from 2011</page_Title>
<CustomParam1>Parameter value</CustomParam1>
</Page2>
</root>
As it's clear that the ElementNode names and sub xml node names are not uniform in nature, it can be any names [xml tags[ and it can be any number of subitems 很明显,ElementNode名称和子xml节点名称本质上不一致,它可以是任何名称[xml标签[并且它可以是任意数量的子项目]
The idea of XML conversion is to enable translation for an application where final object is a Dic XML转换的想法是为最终对象为Dic的应用程序启用转换
What is the best way to do conversion from XML to key-value pair list 从XML转换为键值对列表的最佳方法是什么?
I tried this 我试过这个
var maping = doc.Descendants("Page1")
.Elements()
.ToDictionary(d => (string)d.Name.LocalName,
d => (string)d.Value);
But its returning for only one set page1 Can we make it for all children's of root 但它只返回一个页面1我们可以为所有孩子的根目录
Using this approach i am able to read it to a class with 3 properties 使用这种方法,我能够将它读取到具有3个属性的类
var maping = doc.Descendants("Page1")
.Elements().Select(d => new
{
Attribute = (string)d.Name.LocalName,
Value = d.Value, // equal to id you are searching for
Key = (d.Attribute("alias") != null) ? d.Attribute("alias").Value : ""
}).ToList();
foreach (var s in maping)
{
string ss = string.Format("{0} - {1} && {2}", s.Key, s.Value, s.Attribute);
}
Not sure if you already tried this, 不确定你是否已经尝试过这个,
var doc = XDocument.Load(yourXML);
var maping = doc.Root.Elements()
.ToDictionary(a => (string)a.Attribute("key"),
a => (string)a.Attribute("replaceWith"));
There are other ways too, I found this more fast. 还有其他方法,我发现这更快。
Recursive LINQ query will help you to form hierarchical structure, 递归LINQ查询将帮助您形成层次结构,
public class AwesomeXml
{
public static void Main()
{
string xml = @"<root>
<StandardValues>
<ButtonYES>Ja</ButtonYES>
<ButtonNO>Nei</ButtonNO>
</StandardValues>
<Page1>
<Key_Head>2011 Applications</Key_Head>
<Key_Title>Title from 2011</Key_Title>
<Key_Param1>Parameter value</Key_Param1>
</Page1>
<Page2>
<Page_Head>2011 Applications</Page_Head>
<page_Title>Title from 2011</page_Title>
<CustomParam1>Parameter value</CustomParam1>
</Page2>
</root>";
var doc = XDocument.Parse(xml);
var result = ConvertXmlToDic(doc.Root);
Console.Read();
}
private static object ConvertXmlToDic(XElement element)
{
var result = element.Elements().
Select(
e => new { Key = e.Name,
Value = (e.Descendants().Count() == 0) ? e.Value : ConvertXmlToDic(e)
}
).ToDictionary(e => e.Key, e => e.Value);
return result;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.