2015-10-14 53 views
1

中單獨打印它們我有一個帶有鍵/值對的XML文件。我現在用使用鍵值pairand在xml中讀取節點中的所有值在

XML文件:

<?xml version="1.0" encoding="UTF-8"?> 
<MAN_J6_Variant> 
    <play_music_media_HK> 
     <key name ="Media_Press" value="echo 'Media::1' >> /pps/can/test_pps"/> 
     <key name ="Media_Release" value="echo 'Media::0' >> /pps/can/test_pps"/> 
     <key name ="Media_Source" value ="./fs/etfs/Touch_new -X=258 -Y=26 -display=4 -relTimeout=0.5 " /> 
     <key name ="USB" value ="./fs/etfs/Touch_new -X=480 -Y=134 -display=4 -relTimeout=0.5 " /> 
    </play_music_media_HK> 
    <softkeys> 
     <key name ="Media_SK" value ="./fs/etfs/Touch_new -X= -Y = -display= -relTimeout= " /> 
    </softkeys> 
    <play_music_media_SK> 
     <key name ="bottom_home" value ="./fs/etfs/Touch_new -X=755 -Y=455 -display=4 -relTimeout=0.5 " /> 
     <key name ="HS3" value ="./fs/etfs/Touch_new -X=464 -Y=27 -display= -relTimeout= " /> 
     <key name ="Media_Source" value ="./fs/etfs/Touch_new -X=258 -Y=26 -display=4 -relTimeout=0.5 " /> 
     <key name ="USB" value ="./fs/etfs/Touch_new -X=480 -Y=134 -display=4 -relTimeout=0.5 " /> 
    </play_music_media_SK> 
    <Inside_media> 
     <key name ="Media_Source" value ="./fs/etfs/Touch_new -X= -Y = -display= -relTimeout= " /> 
     <key name ="Browse" value ="./fs/etfs/Touch_new -X= -Y = -display= -relTimeout= " /> 
     <key name ="Audio_playing" value ="./fs/etfs/Touch_new -X= -Y = -display= -relTimeout= " /> 
    </Inside_media> 
    <Inside_Media_source> 
     <key name ="USB" value ="./fs/etfs/Touch_new -X= -Y = -display= -relTimeout= " /> 
     <key name ="SD1" value ="./fs/etfs/Touch_new -X= -Y = -display= -relTimeout= " /> 
     <key name ="SD2" value ="./fs/etfs/Touch_new -X= -Y = -display= -relTimeout= " /> 
     <key name ="USB1" value ="./fs/etfs/Touch_new -X= -Y = -display= -relTimeout= " /> 
     <key name ="IPOD" value ="./fs/etfs/Touch_new -X= -Y = -display= -relTimeout= " /> 
    </Inside_Media_source> 
    <Inside_Media_Browse> 
     <key name ="Artist" value ="./fs/etfs/Touch_new -X= -Y = -display= -relTimeout= " /> 
     <key name ="Albums" value ="./fs/etfs/Touch_new -X= -Y = -display= -relTimeout= " /> 
    </Inside_Media_Browse> 
</MAN_J6_Variant> 

我需要打印在單獨的行節點play_music_media_SK所有的值。

我寫了下面的代碼在LINQ

var play_music_media_SK = doc.Descendants("play_music_media_SK").Select(x => new { play_music_media_SK = x.Attribute("name").Value, name = x.Attribute("value").ToString() }); 


//TestSetup.LogFile.WriteMsg("values in softkey"+taps); 

    foreach (var value in play_music_media_SK) 
    { 
    console.writeline(value); 
    } 

但是,這是行不通的。

+0

你有機會看看答案嗎? –

回答

0

這應該給你正確的結果: -

var result = doc.Root.Element("play_music_media_SK") 
         .Elements("key").Select(x => (string)x.Attribute("value")); 

    foreach (var item in result) 
    { 
     Console.WriteLine(item); 
    } 

方法:從根節點搜索play_music_media_SK元素,裏面那個找key元素(使用Elements因爲所有都是play_music_media_SK直接子),那麼最後投影屬性value