VB.NET XML Parser - 查找具有特定属性的元素 &价值 [英] VB.NET XML Parser - Find elements with specific attribute & value

查看:25
本文介绍了VB.NET XML Parser - 查找具有特定属性的元素 &价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 VB.NET 读取 XHTML 文件.我需要我的程序来查找 XML 结构中的每个元素,这些元素将class"属性设置为特定值.

I am using VB.NET to read an XHTML file. I need my program to find every element within the XML structure which has the "class" attribute set to a specific value.

IE:我需要文档中包含 class="mytag" 的所有元素的数组(或类似列表).

IE: I need an array (or similar list) of all elements from the document which contain class="mytag".

有一个我需要检测的几个值的列表,所有这些值都以同一个词mytag"开头,后跟另一个词 - 所以在这里使用包含"函数似乎是明智的.

There is a list of several values I need to detect, all of which start with the same word 'mytag' followed by another word - so using a "contains" function seems sensible here.

对我解析这些属性的方法的任何建议将不胜感激.干杯.

Any suggestions of ways I can parse out these attributes would be appreciated. Cheers.

推荐答案

使用以下 XPath 表达式与 SelectNodesXmlDocument<的代码>函数/code> 对象:

Use the following XPath expression with the SelectNodes function of an XmlDocument object:

//*[@class="mytag"]

此表达式将选择 class 属性值等于 "mytag" 的任何 XHTML 元素.

This expression will select any XHTML element whose class attribute's value is equal to "mytag".

如果您想查找 class 属性包含特定字符串的所有元素,XPath 函数 contains 将帮助您:

If you want to find all elements whose class attribute contains a particular string, the XPath function contains will help you:

//*[contains(@class,"mytag")]

SelectNodes 函数从对应于作为参数传递给它的 XPath 表达式的 XmlDocument 返回一个 XML 节点列表.根据您的描述,这正是您所需要的.

The SelectNodes function returns a list of XML nodes from the XmlDocument corresponding on the XPath expression passed to it as a parameter. Based on your description, it's exactly what you need.

这篇关于VB.NET XML Parser - 查找具有特定属性的元素 &amp;价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆