[英]c# same tag name on a xml element
i got this case, i get all the elements on a xmlnodelist using the function getelementesbytagname(""), but sometimes i can had something like this. 我遇到这种情况,我使用功能getelementesbytagname(“”)获得了xmlnodelist上的所有元素,但是有时我可以拥有类似的东西。
<?xml version="1.0" encoding="UTF-8" ?>
<Element xsi:schemaLocation="http://localhost/AML/CaseInvestigationMangement/Moduli/XmlImportControls/xsdBorrow.xsd xsd2009027_kor21.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<nodo>
<nombre>nodo1</nombre>
<dato>1</dato>
<otronodo>
<nombre>nododentrodenodo</nombre>
<dato2>23</dato2>
</otronodo>
</nodo>
<nodo>
...
</nodo>
</Element>
if y search all the nodes by the name "nombre" well i get the nodo nombre and the otronodo nombre. 如果您通过名称“ nombre”搜索所有节点,那么我将获得nodo nombre和otronodo nombre。
can i get only the name of the nodo node? 我只能获取nodo节点的名称吗?
I think you want only the nombre
values that have a nodo
parent, but it's possible you want any nombre
from any child of Element
. 我认为您只需要具有nodo
父级的nombre
值,但是有可能希望从Element
任何子级获得任何nombre
。 With XPath and SelectNodes
, you can do either, so I've included both below. 使用XPath和SelectNodes
,您都可以做,所以我在下面都包括了两者。 SelectNodes
returns XmlNodeList
, just like GetElementsByTagName()
does. SelectNodes
返回XmlNodeList
,就像GetElementsByTagName()
一样。
var doc = new XmlDocument();
doc.LoadXml(@"<?xml version=""1.0"" encoding=""UTF-8"" ?>
<Element>
<nodo>
<nombre>nodo1</nombre>
<dato>1</dato>
<otronodo>
<nombre>nododentrodenodo</nombre>
<dato2>23</dato2>
</otronodo>
</nodo>
<nodo>
<nombre>nodo2</nombre>
</nodo>
<frodo>
<nombre>frodo nodo</nombre>
</frodo>
</Element>
");
// Any nombre whose parent is a nodo
var nodosNombres = doc.DocumentElement.SelectNodes("//nodo/nombre");
// Any nombre belonging to any child of Element
var topNombres = doc.DocumentElement.SelectNodes("/Element/*/nombre");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.