Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XML Parsing in VBNet

I'm trying to use VB.NET to extract the innertext from <itemnote> based upon the name attribute of <series> and the name attribute of it's child element <item>. I can extract the name attributes but I just can't access the innertext of <itemnote>

Here is some sample XML:

<?xml version="1.0" encoding="UTF-8"?>
<Products>
  <series name="HiTech" year="2000" country="United Kingdom">
    <item name="Robotic" collected="no">
    <itemnote>Exclusive UK Release</itemnote>
    </item>
     <item name="Future" collected="yes">
    <itemnote>LED Edition</itemnote>
    </item>
  </series>
  <series name="Neo" year="2003" country="United Kingdom">
    <item name="X Rated" collected="no">
    <itemnote>Red striped version</itemnote>
    </item>
     <item name="Cool Breeze" collected="no">
    <itemnote>Includes promo booklet</itemnote>
    </item>
  </series>
</Products>

So far I've tried using a recursive loop with XDocument but I've been unable to get anything more than a list of all the <series> and <item> attributes. I've also tried using XPath and Linq but I can't figure out how to make either of those do as I wish. Any help would be great! Cheers.

like image 650
Steve Jones Avatar asked Nov 13 '22 14:11

Steve Jones


1 Answers

You need to get the reference of <item> nodes and based upon item element, you can compare values of name attributes of parent and child element.

Dim nameOfSeries = "HiTech"
Dim nameOfItem = "Future"

Dim result = doc.Descendants("item").Where(Function(p)
                               Return p.Parent.Attribute("name").Value = nameOfSeries And 
                                                p.Attribute("name").Value = nameOfItem
                               End Function)

IF result.Count Then
  For Each ele In result
    Console.WriteLine(ele.Element("itemnote").Value)
  Next
End If
like image 51
KV Prajapati Avatar answered Nov 15 '22 05:11

KV Prajapati