将 Xml 节点子元素作为字典检索
我想检索以下 xml 的子元素(属性)作为字典 使用 LINQ 到 XML?
因此,当调用时,
Dictionary<String,String> dict = ReadFromXml("TC_001","L3")
我应该能够检索 ControlList id="TC_001" 的 Control uid="L3" 作为 字典中的名称、值对为
["id","googlelink"]
[“姓名”,空]
[“类”,空]
.
.
。
<?xml version="1.0"?>
<ControlList id="TC_001">
<Control uid="L1">
<Property name="Id"> <![CDATA[googlelink]]></Property>
<Property name="Name"> null </Property>
<Property name="Target"> null </Property>
<Property name="Innertext"> <![CDATA["Try searching me www.google.com"]]></Property>
<Property name="Href"> <![CDATA["http://www.google.com/"]]></Property>
</Control>
<Control uid="L2">
<Property name="Id"> <![CDATA[googlelink]]></Property>
<Property name="Name"> null </Property>
<Property name="Class"> null </Property>
<Property name="ControlDefinition"> <![CDATA["id=googlelink href=\"http://www.google.co"]]> </Property>
<Property name="TagInstance"> 1 </Property>
</Control>
<Control uid="L3">
<Property name="Id"> <![CDATA[googlelink]]></Property>
<Property name="Name"> null </Property>
<Property name="Target"> null </Property>
<Property name="Innertext"> <![CDATA["Try searching me www.google.com"]]></Property>
</Control>
</ControlList>
编辑 8/1 :同样的问题不同的 xml 结构。 (节点现在没有名称“Property”)
<?xml version="1.0"?>
<ControlList id="TC_001">
<Control uid="L1">
<Id> <![CDATA[googlelink]]><Id>
<Name> null </Name>
<Target> null </Target>
<Innertext> <![CDATA["Try searching me www.google.com"]]></Innertext>
</Control>
<!-- more multiple controls similar to above-->
</ControlList>
I want to retrieve the child element (attributes) of the following xml as a Dictionary
using LINQ-to-XML??
So when called
Dictionary<String,String> dict = ReadFromXml("TC_001","L3")
i should be able to retrieve Control uid="L3" of ControlList id="TC_001" as
name,value pair in dictionary as
["id","googlelink"]
["name",null]
["class",null]
.
.
.
<?xml version="1.0"?>
<ControlList id="TC_001">
<Control uid="L1">
<Property name="Id"> <![CDATA[googlelink]]></Property>
<Property name="Name"> null </Property>
<Property name="Target"> null </Property>
<Property name="Innertext"> <![CDATA["Try searching me www.google.com"]]></Property>
<Property name="Href"> <![CDATA["http://www.google.com/"]]></Property>
</Control>
<Control uid="L2">
<Property name="Id"> <![CDATA[googlelink]]></Property>
<Property name="Name"> null </Property>
<Property name="Class"> null </Property>
<Property name="ControlDefinition"> <![CDATA["id=googlelink href=\"http://www.google.co"]]> </Property>
<Property name="TagInstance"> 1 </Property>
</Control>
<Control uid="L3">
<Property name="Id"> <![CDATA[googlelink]]></Property>
<Property name="Name"> null </Property>
<Property name="Target"> null </Property>
<Property name="Innertext"> <![CDATA["Try searching me www.google.com"]]></Property>
</Control>
</ControlList>
Edit 8/1 : Same question different xml structure.
(nodes dont have name "Property" now)
<?xml version="1.0"?>
<ControlList id="TC_001">
<Control uid="L1">
<Id> <![CDATA[googlelink]]><Id>
<Name> null </Name>
<Target> null </Target>
<Innertext> <![CDATA["Try searching me www.google.com"]]></Innertext>
</Control>
<!-- more multiple controls similar to above-->
</ControlList>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
下面是一些 C# 示例:
假设传入的 id 始终存在于传入的 XML 中,否则 First() 调用将引发异常。
[编辑]对于更改后的 XML 结构,您可以使用该方法的以下改编:
Here is some C# sample:
That assumes the ids passed in are always present in the XML passed in, otherwise the First() calls will throw an exception.
[edit]For the changed XML structure you could use the following adaption of the method: