2011-01-07 50 views
0

我想使用LINQ-to-XML檢索下列xml的子元素(屬性)作爲字典 ?檢索Xml節點子元素作爲詞典

所謂當

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

我應該能夠檢索控制 UID = 「L3」 ControlList ID = 「TC_001」 作爲 名稱,在字典值對作爲

[「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> 

編輯8/1:同樣的問題不同的xml結構。 (節點不具有名稱「屬性」現在)

<?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

下面是一些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,否則,第( )調用會拋出異常。

[編輯]對於你可以使用的方法的適應之後更改的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; }); 
} 
+0

是的ID將始終存在 – Amitd 2011-01-08 02:12:10