如何使用 XDocument 查询获取正确的元素
How to get the right elements with XDocument query
我在尝试使用 XDocument 解析 XML 文件时无法正确查询。
我想要的:
给我所有 "Name" 步骤元素的值,id = "id_3" 所以结果应该是一个列表,其中包含“第 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>
我尝试过这样的事情(在 Unity3D 中)。
IEnumerable<XElement> e_nameOfObjects =
from el in myXMLDoc.Root.Elements ("Step")
where (string)el.Attribute ("id") == "id_" + currentStep
select el.Elements ("Component");
foreach (XElement e in e_nameOfObjects) {
Debug.Log (e.Element("Name"));
}
错误信息:
Cannot implicitly convert type
System.Collections.Generic.IEnumerable>'
to System.Collections.Generic.IEnumerable'. An explicit conversion
exists (are you missing a cast?)
您可以使用 Elements
方法获取所有组件,然后在这些组件中 select Name
像这样:-
var names = myXMLDoc.Descendants("Step")
.Where(x => (string)x.Attribute("id") == "id_3")
.Elements("Component")
.Select(x => (string)x.Element("Name"));
在这里,names 将 return IEnumerable<string>
您可以在其中轻松地遍历 foreach 循环。
我在尝试使用 XDocument 解析 XML 文件时无法正确查询。
我想要的:
给我所有 "Name" 步骤元素的值,id = "id_3" 所以结果应该是一个列表,其中包含“第 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>
我尝试过这样的事情(在 Unity3D 中)。
IEnumerable<XElement> e_nameOfObjects =
from el in myXMLDoc.Root.Elements ("Step")
where (string)el.Attribute ("id") == "id_" + currentStep
select el.Elements ("Component");
foreach (XElement e in e_nameOfObjects) {
Debug.Log (e.Element("Name"));
}
错误信息:
Cannot implicitly convert type System.Collections.Generic.IEnumerable>' to System.Collections.Generic.IEnumerable'. An explicit conversion exists (are you missing a cast?)
您可以使用 Elements
方法获取所有组件,然后在这些组件中 select Name
像这样:-
var names = myXMLDoc.Descendants("Step")
.Where(x => (string)x.Attribute("id") == "id_3")
.Elements("Component")
.Select(x => (string)x.Element("Name"));
在这里,names 将 return IEnumerable<string>
您可以在其中轻松地遍历 foreach 循环。