简体   繁体   English

如何使用SelectNode()WITHOUT属性检索XML节点列表?

[英]How to retrieve list of XML Nodes using SelectNode() WITHOUT attributes?

I'm trying to parse out a simple list of strings from an XML file. 我正在尝试从XML文件中解析出一个简单的字符串列表。 I only want to retrieve the values that do NOT have a specified attribute. 我只想检索没有指定属性的值。

For example, in this XML snippet: 例如,在此XML片段中:

<meaning>sultry</meaning>
<meaning>hot</meaning>
<meaning>summer heat</meaning>
<meaning m_lang="fr">chaud</meaning>
<meaning m_lang="fr">torride</meaning>
...

Calling SelectNodes("//meaning") will return sultry, hot, summer heat, chaud, torride. 调用SelectNodes("//meaning")将返回闷热,炎热,夏季炎热,chaud,torride。 However, I only want to retrieve the values labeled only <meaning> , not <meaning m_lang="fr> , ie I only want to have sultry, hot, summer heat. 但是,我只想检索仅标记为<meaning>的值,而不是<meaning m_lang="fr> ,即我只想要闷热,炎热,夏天的热量。

Is there a simple method to only retrieve the non-attribute values? 有一种简单的方法只能检索非属性值吗? They will always be the first in the list, but there may be a single value, or multiple. 它们将始终是列表中的第一个,但可能只有一个值或多个值。

Yes, you'll need to make your XPath Expression (the string you're passing to SelectNodes()) more specific. 是的,您需要使您的XPath表达式 (您传递给SelectNodes()的字符串)更具体。 In this case, you want to find nodes without any additional attributes, so you modify your XPath expression like so: 在这种情况下,您希望找到没有任何其他属性的节点,因此您可以像这样修改XPath表达式:

SelectNodes("//meaning[not(@*)]")

To break the above down: 打破以上:

  • "meaning" selects all "meaning" elements, “意义”选择所有“意义”元素,

  • "@*" selects all elements with at least one attribute, “@ *”选择具有至少一个属性的所有元素,

  • "not" negates a rule “不”否定了一条规则

So together this means "Select all 'meaning' elements that do not have at least one attribute" . 因此,这意味着“选择所有”意味着“至少没有一个属性的元素”。

For further reference, check out this nifty xpath cheatsheet . 如需进一步参考,请查看这个漂亮的xpath cheatsheet

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM