Reading XML file - VB.NET

G

Guest

Hi,

I'm trying to read an XML file using VB.NET and I can't get the XPath
queries to work. Here's a snippet of the XML file I'm using and the code:

<?xml version="1.0" encoding="UTF-8"?>
<job xmlns="http://ns.real.com/tools/job.2.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://ns.real.com/tools/job.2.0
http://ns.real.com/tools/job.2.0.xsd">
<clipInfo>
<entry>
<name>Author</name>
<value type="string">This is the author</value>
</entry>

Dim doc As New XmlDocument, root As XmlElement, entryList As
XmlNodeList
doc.Load("job.xml")

Dim nsmgr As New XmlNamespaceManager(doc.NameTable)
nsmgr.AddNamespace(String.Empty, "http://ns.real.com/tools/job.2.0")
nsmgr.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance")
root = doc.DocumentElement

entryList = root.SelectNodes("/job/clipInfo/entry")
For Each x As XmlNode In entryList
MsgBox(x.InnerXml)
Next

When I run this zero nodes are returned. I think it has something to do with
the namespaces but I don't know how to fix it (I tried reading the document
without using an XMLNameSpaceManager and it still didn't work). Any help
would be greatly appreciated!
 
K

Kevin Yu [MSFT]

Hi Jonathan,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you're trying to select the nodes using
an XPath query. If there is any misunderstanding, please feel free to let
me know.

In .NET xml, when we use an XPath query, it is required to use a qualified
node name. So we have to add prefix to node names whatever they are under
default namespace of non-default ones. Also, we need to give the
namespacemanager object to SelectNodes as the second argument. Here I made
some changes to your code. HTH.

Dim doc As New XmlDocument, root As XmlElement, entryList As
XmlNodeList
doc.Load("job.xml")

Dim nsmgr As New XmlNamespaceManager(doc.NameTable)
nsmgr.AddNamespace("d", "http://ns.real.com/tools/job.2.0")
nsmgr.AddNamespace("xsi",
"http://www.w3.org/2001/XMLSchema-instance")
root = doc.DocumentElement

entryList = root.SelectNodes("/d:job/d:clipInfo/d:entry", nsmgr)
For Each x As XmlNode In entryList
MsgBox(x.InnerXml)
Next

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
G

Guest

Thanks Kevin, that part is working now. I've run into another issue
though...here's the XML and code:

<clipInfo>
<entry>
<name>Author</name>
<value type="string">This is the author</value>
</entry>

I need to select all entry elements where name="Author"...from what i've
read of XPath online this line should work but it doesn't:
entryList =
root.SelectNodes("/d:job/d:clipInfo/d:entry[/d:name='Author']", nsmgr)

Thanks for your help! :)
 
K

Kevin Yu [MSFT]

Hi Jonathan,

Removing the slash will make your code work:

XmlNodeList entryList =
root.SelectNodes("/d:job/d:clipInfo/d:entry[d:name='Author']", nsmgr);

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top