Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/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# 如何使用XDocument查询获取正确的元素_C#_Xml_Linq To Xml - Fatal编程技术网

C# 如何使用XDocument查询获取正确的元素

C# 如何使用XDocument查询获取正确的元素,c#,xml,linq-to-xml,C#,Xml,Linq To Xml,在尝试使用XDocument解析XML文件时,我很难获得正确的查询 我想要的是: 使用id=“id_3”为我提供步骤“Name”元素的所有值,因此结果应该是一个包含“第1部分的名称、第2部分的名称、第3部分的名称、第4部分的名称”的列表 输入XML: <MyXML> <Step id="id_1" type="type1"> <Name>Some Name</Name> <Location>1</Loca

在尝试使用XDocument解析XML文件时,我很难获得正确的查询

我想要的是:

使用id=“id_3”为我提供步骤“Name”元素的所有值,因此结果应该是一个包含“第1部分的名称、第2部分的名称、第3部分的名称、第4部分的名称”的列表

输入XML:

<MyXML>
   <Step id="id_1" type="type1">
     <Name>Some Name</Name>
     <Location>1</Location>
     <Quantity>1</Quantity>
   </Step>
   <Step id="id_2" type="type1">
     <Name>>Some Name</Name>
     <Location>2</Location>
     <Quantity>1</Quantity>
   </Step>
   <Step id="id_3" type="type2">
     <Instruction>This is some text</Instruction>
     <Component>
       <Name>Name of part 1</Name> // --> I want this value
       <Transition_Start>-0.2,0.01,0.0</Transition_Start>
       <Transition_End>0,0.01,0.0</Transition_End>
     </Component>
     <Component>
       <Name>Name of part 2</Name> // --> and this
       <Transition_Start>0.2,0.01,0</Transition_Start>
       <Transition_End>0,0.01,0</Transition_End>
     </Component>
     <Component>
       <Name>Name of part 3</Name> // --> and this
       <Transition_Start>0.05,0.1004,0.0333</Transition_Start>
       <Transition_End>-0.0803,0.1004,0.0333</Transition_End>
     </Component>
     <Component>
       <Name>Name of part 4</Name> // --> and this
       <Transition_Start>-0.0107,0.0383,-0.2328</Transition_Start>
       <Transition_End>-0.0107,0.0383,-0.2328</Transition_End>
     </Component>
   </Step>
 </MyXML>

某个名字
1.
1.
>某个名字
2.
1.
这是一些文本
第1部分的名称//-->我需要此值
-0.2,0.01,0.0
0,0.01,0.0
第2部分的名称//-->以及
0.2,0.01,0
0,0.01,0
第3部分的名称//-->以及
0.05,0.1004,0.0333
-0.0803,0.1004,0.0333
第4部分的名称//-->以及
-0.0107,0.0383,-0.2328
-0.0107,0.0383,-0.2328
我尝试过类似的东西(在Unity3D中)

IEnumerable e_nameofObject=
从myXMLDoc.Root.Elements中的el(“步骤”)
其中(字符串)el.Attribute(“id”)==“id_389;”+currentStep
选择el.元素(“组件”);
foreach(对象名称中的元素e){
Log(e.Element(“Name”));
}
错误消息:

无法隐式转换类型 System.Collections.Generic.IEnumerable>' 到System.Collections.Generic.IEnumerable.的显式转换 存在(您是否缺少演员阵容?)


您可以使用
元素
方法获取所有组件,然后在这些组件内选择
名称
,如下所示:-

var names = myXMLDoc.Descendants("Step")
            .Where(x => (string)x.Attribute("id") == "id_3")
            .Elements("Component")
            .Select(x => (string)x.Element("Name"));
在这里,name将返回
IEnumerable
,您可以在其上轻松迭代foreach循环


请描述它以何种方式不起作用?您是否获得了错误的数据、异常或其他信息?请在问题中详细说明。“请在问题中详细说明”(注释中的代码/错误太难阅读)。IEnumerable e_nameOfObjects=(来自myXMLDoc.Root.Elements(“步骤”)中的el,其中(字符串)el.Attribute(“id”)==“id\u”+currentStep select el.Elements(“Component”).ToList();谢谢。但这仍然会给我以下错误消息:无法将type
System.Collections.Generic.List”隐式转换为
System.Collections.Generic.IEnumerable。存在显式转换(是否缺少转换?)如果维度不匹配,查询将返回一个元素列表
var names = myXMLDoc.Descendants("Step")
            .Where(x => (string)x.Attribute("id") == "id_3")
            .Elements("Component")
            .Select(x => (string)x.Element("Name"));