将Xml节点子元素检索为字典

时间:2011-01-07 08:22:54

标签: linq-to-xml

我想将以下xml的子元素(属性)检索为Dictionary 使用LINQ-to-XML ??

所以当被召唤时

Dictionary<String,String> dict = ReadFromXml("TC_001","L3")  

我应该能够检索 ControlList id =“TC_001”的控制 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>

1 个答案:

答案 0 :(得分:1)

这是一些C#示例:

static void Main()
{
    XDocument controls = XDocument.Load(@"..\..\XMLFile1.xml");
    string id1 = "TC_001", id2 = "L3";

    Dictionary<string, string> props =
        ReadFromXml(controls, id1, id2);
    foreach (string key in props.Keys)
    {
        Console.WriteLine("{0}: {1}", key, props[key]);
    }
}
static Dictionary<string, string> ReadFromXml(XDocument controlDoc, string listId, string controlId)
{
    return controlDoc
        .Elements("ControlList")
        .First(c => (string)c.Attribute("id") == listId)
        .Elements("Control")
        .First(c => (string)c.Attribute("uid") == controlId)
        .Elements("Property")
        .ToDictionary(p => (string)p.Attribute("name"),
        p => { string value = p.Value.Trim(); return value == "null" ? null : value; });
}

假设传入的id总是存在于传入的XML中,否则First()调用将抛出异常。

[edit]对于已更改的XML结构,您可以使用以下方法修改:

static Dictionary<string, string> ReadFromXml(XDocument controlDoc, string listId, string controlId)
{
    return controlDoc
        .Elements("ControlList")
        .First(c => (string)c.Attribute("id") == listId)
        .Elements("Control")
        .First(c => (string)c.Attribute("uid") == controlId)
        .Elements()
        .ToDictionary(el => el.Name.LocalName,
        el => { string value = el.Value.Trim(); return value == "null" ? null : value; });
}