Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 以xml格式读取一个节点中的所有值(键值对),并在中以单独的行打印它们_C#_Linq_Linq To Xml - Fatal编程技术网

C# 以xml格式读取一个节点中的所有值(键值对),并在中以单独的行打印它们

C# 以xml格式读取一个节点中的所有值(键值对),并在中以单独的行打印它们,c#,linq,linq-to-xml,C#,Linq,Linq To Xml,我有一个带有键/值对的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

我有一个带有键/值对的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>

但这不起作用。

这应该会给您提供正确的结果:-

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\u music\u media\u SK
元素,在该元素中查找
元素(使用
元素
,因为所有元素都是
play\u music\u media\u SK
的直接子元素),然后最终投影属性
,这应该会给您正确的结果:-

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\u music\u media\u SK
元素,在该元素中查找
元素(使用
元素
,因为所有元素都是
play\u music\u media\u SK
的直接子元素)最后投射属性

你有机会看答案吗?你有机会看答案吗?