Home > Mobile >  VB.net XMLNodeList returns 0 when using document.selectNodes(XPath)
VB.net XMLNodeList returns 0 when using document.selectNodes(XPath)

Time:08-22

I have an XML doc:

<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.ABCdefg.com/XYZ"
                 xmlns:doc="http://www.ABCdefg.com/LMN"
                 xmlns:history="http://www.ABCdefg.com/LMNHistory">
    
    <import href="ENTypes.xml"/>
    <import href="ENSTR.xml"/>
    <import href="ENNUM.xml"/>
    <types>
        <!--Operations definitions  -->
            <type javaPrefix="Seg" name="James" javaPackage="com.ABCdefg.jk.ox">
                <documentation>
                 A collection of entity creation requests
                </documentation>
                <member name="apiHeader" type="ApiHeaderType" use="optional">
                    <doc:required  r="" c="" u="" />
                    <documentation>
                    Operation header.
                    </documentation>
                </member>
                <member name="EN" type="ENTypes" collection="true">
                    <doc:required  r="" c="" u="" />
                    <documentation>
                       A collection of entities.
                    </documentation>
                </member>
            </type>
    </types>
</definitions>

I am trying to get the list of Types in the XML document above. I am using .SelectNodes to get the nodes. See the following code:

Dim doc As XmlDocument() 
doc.load("XmlPath")
Dim NodeList As XmlNodeList = doc.SelectNodes("definitions/types/*)

The NodeList variable has a count of 0. For other xml files, this code worked, but it is not working for this set of xml files

CodePudding user response:

Looks like you have a typo here.
Instead of

definitions/Types/*

it should be

definitions/types/*

the later expression works.
You can validate that here

CodePudding user response:

Here is solution using Xml Linq

Imports System
Imports System.Xml
Imports System.Xml.Linq

Module Program
    Const FILENAME As String = "c:\temp\test.xml"
    Sub Main(args As String())
        Dim doc As XDocument = XDocument.Load(FILENAME)
        Dim ns As XNamespace = doc.Root.GetDefaultNamespace()

        Dim results = doc.Descendants(ns   "type") _
           .Select(Function(x) New With {
                    .Type = CType(x.Attribute("javaPrefix"), String),
                    .name = CType(x.Attribute("name"), String),
                    .package = CType(x.Attribute("javaPackage"), String)
                }).ToList()
    End Sub
End Module
  • Related