2011-01-07 21 views
0

LINQ-to-XMLを使用して、次のxmlの子要素(属性)を辞書 として取得しますか?Xmlノードの子要素を辞書として取得する

として

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

呼び出されたときに私は 名としてControlList ID = "TC_001" のコントロールはuid = "L3" を取得することができるはず、辞書内の値のペア["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

+0

THX常に存在するだろう...私も..how缶編集を追加しました私は既存の答えを変更して "編集"部分を行う。 – Amitd

+0

wow thx :) great ... linqとちょっと混乱しましたが、今は素晴らしいようです。 – Amitd

関連する問題