471,338 Members | 1,009 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,338 software developers and data experts.

Reading XML file - VB.NET

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!
--
Jonathan Aneja
Jul 22 '05 #1
3 7141
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."

Jul 22 '05 #2
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! :)
--
Jonathan Aneja
"Kevin Yu [MSFT]" wrote:
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."

Jul 22 '05 #3
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."

Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Xah Lee | last post: by
1 post views Thread by fabrice | last post: by
19 posts views Thread by Lionel B | last post: by
4 posts views Thread by Oliver Knoll | last post: by
6 posts views Thread by Rajorshi Biswas | last post: by
7 posts views Thread by John Dann | last post: by
6 posts views Thread by efrenba | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.