473,387 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 7247
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Xah Lee | last post by:
# -*- coding: utf-8 -*- # Python # to open a file and write to file # do f=open('xfile.txt','w') # this creates a file "object" and name it f. # the second argument of open can be
1
by: fabrice | last post by:
Hello, I've got trouble reading a text file (event viewer dump) by using the getline() function... After 200 - 300 lines that are read correctly, it suddenly stops reading the rest of the...
19
by: Lionel B | last post by:
Greetings, I need to read (unformatted text) from stdin up to EOF into a char buffer; of course I cannot allocate my buffer until I know how much text is available, and I do not know how much...
4
by: Oliver Knoll | last post by:
According to my ANSI book, tmpfile() creates a file with wb+ mode (that is just writing, right?). How would one reopen it for reading? I got the following (which works): FILE *tmpFile =...
6
by: Rajorshi Biswas | last post by:
Hi folks, Suppose I have a large (1 GB) text file which I want to read in reverse. The number of characters I want to read at a time is insignificant. I'm confused as to how best to do it. Upon...
1
by: Need Helps | last post by:
Hello. I'm writing an application that writes to a file a month, day, year, number of comments, then some strings for the comments. So the format for each record would look like:...
7
by: John Dann | last post by:
I'm trying to read some binary data from a file created by another program. I know the binary file format but can't change or control the format. The binary data is organised such that it should...
5
blazedaces
by: blazedaces | last post by:
Ok, so you know my problem, java is running out of memory reading with SAX, the event-based xml parser intended more-so than DOM for extremely large files. I'll try to explain what I've been doing...
6
by: efrenba | last post by:
Hi, I came from delphi world and now I'm doing my first steps in C++. I'm using C++builder because its ide is like delphi although I'm trying to avoid the vcl. I need to insert new features...
2
by: Derik | last post by:
I've got a XML file I read using a file_get_contents and turn into a simpleXML node every time index.php loads. I suspect this is causing a noticeable lag in my page-execution time. (Or the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.