简体   繁体   中英

How do I select XML elements in this situation in VB.net?

So I have:

<rss version="2.0">
    <channel>
        <title>My channel</title>
        <item></item>
        <item></item>
        <item></item>
        <item></item>
    </channel>
</rss>

When I use xmlDocument to parse it, I try to get all the item elements, but if I use

For Each item As System.Xml.XmlElement In xmlDocument.Item("rss").Item("channel")

It would give me 5 instead 4 results, since <title>My Channel</title> is considered one of the item under channel. I'm just wondering if there is anyway to loop through only the 4 item elements. Thanks!

You can use XPath expression to drilldown directly to elements you need. In your case it would be:

For Each item As System.Xml.XmlElement In XmlDocument.SelectNodes("/rss/channel/item")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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