[英]Select specific node from XML document when there is two or more nodes with same name C#
Is there a way to select specific node of XML file when you have nodes with same name more then once? 当节点具有相同的名称(一次以上)时,是否可以选择XML文件的特定节点?
For example I want to select the value of the node with name IBAN. 例如,我想选择名称为IBAN的节点的值。 But I have it twice as a child of some other two nodes.
但是作为其他两个节点的子节点,我有两次。
I am getting node with name IBAN, put it gets first one of course. 我正在获取名称为IBAN的节点,当然,它是第一个。
public string GetIBANValueFromXML(XmlDocument xmlDoc)
{
string ibanValue = "";
XmlNodeList xnList = xmlDoc.SelectNodes("/Element[@*]");
if (xnList != null)
{
foreach (XmlNode xn in xnList)
{
XmlNode ibanNode = xn.SelectSingleNode("IBAN");
if (ibanNode != null)
{
ibanValue = ibanNode.InnerText;
}
}
}
return ibanValue;
}
If there is a clean way to accomplish this? 是否有一种干净的方法可以做到这一点? To use this functionality but to return second IBAN node value?
使用此功能但返回第二个IBAN节点值?
You can use the XPath /Element/IBAN[2]
to select the second IBAN
child element of the root element named Element
: 您可以使用XPath
/Element/IBAN[2]
选择名为Element
的根元素的第二个IBAN
子Element
:
XmlNode ibanNode = xmlDoc.SelectSingleNode("/Element/IBAN[2]");
if (ibanNode != null) { ... }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.