Question Reading from XML

SmokingCookie

Member
Joined
Mar 26, 2009
Messages
8
Location
Netherlands
Programming Experience
Beginner
Hi,

I'd like some help on this one, 'cause MSDN is (once again) very unclear.

I want to load an XML document, loop through all its nodes (Document.SelectNodes() ? ), retrieve the value of an attribute and its InnerText. Loading the document is no problem, but that's it; I cannot quite rely on MSDN's clarity.

Could someone please help me?
Thanks in advance :)
 
i have no idea if this is right or not, because i'm very new to this, but i just covered this subject today in my own project so may be able to help you?

imagine a hypothetical .XML file like:

HTML:
<books_table>

<book>
<isbn>290239203</isbn>
<title>hello this is a book</title>
<author>me</author>
<class>classy</class>
</book>
</books table>

(only imagine loads of the nodes in there but i can't be bothered to type it up, sorry)


then imagine you have a windows application. add a list box (called listbox1) and a button ('list title and author')

double click the button to open the code editor + type this in:

VB.NET:
Dim doc As New System.Xml.XmlDocument
doc.Load("C:\books.xml")

'add the following code to create an xmlnodelist from the <book> elements and their nested elements:

Dim nodes As System.Xml.XmlNodeList
nodes= doc.SelectNodes("Books_Table/book")

'now add a loop to display the text contained in each <title> and <author> element, then run the application

Dim counter = 0
Do Until counter = nodes.Count
    ListBox1.Items.Add(nodes.Item(counter) _
    .SelectSingleNode("title").InnerText & vbTab _
    & nodes.Item(counter) _
    .SelectSingleNode("author").InnerText & vbCrLf)
    counter += 1
Loop


then tweek it to fit your requirements/variable. you can delete the option to list two node elements but it's better to include two and delete rather than just have one? idkidk. GOOD LUCK :) xox
 
Hello,

You've just helped me solving 80% of the problem, thank you very much on that one :)

Now the other 20%: I am also trying to retrieve an attribute's value. So the XML would look like this:

<books_table>
<book ISBN="290239203">
<title>hello this is a book</title>
<author>me</author>
<class>classy</class>
</book>
</books_table>
Then retrieving the ISBN from the attribute is what remains. Have you got an idea of how that works?

EDIT::

Hmm.. Seems that changing a file path is bad.. I've changed it to D:\Books.xml (and moved the XML file), now I get some weird "file not found"-error.

A first chance exception of type 'System.IO.FileNotFoundException' occurred in System.Xml.dll

A first chance exception of type 'System.IO.FileNotFoundException' occurred in System.Xml.dll
 
Last edited:
ooh, i understand what you're asking but i have NO idea. i'm a complete newbie at this, i was amazed some one posted a question i could answer!

i know that the text between each <> is called 'innertext' so would outertext or something be worth a shot?

XML Tutorial

this website is REALLY detailed all about xml, so you might be able to find the name/reserved words regarding your problem and try and slot them in to replace 'innerword' or something? goodluckgoodluck! sorry i can't be of more help :( xxx
 
Since you already have the node, get the attribute from the Attributes collection of the node.
 
Well, in JavaScript, I usually utilise DOMObject.getAttribute(name). I can't seem to find it in System.Xml. Does such a function exist?
 
Since you already have the node, get the attribute from the Attributes collection of the node.

Here's where JohnH was trying to point you.

VB.NET:
		Dim doc As New XmlDocument
		doc.Load("C:\Temp\Books.xml")

		Dim nodes As XmlNodeList = doc.SelectNodes("books_table/book")

		For i As Integer = 0 To nodes.Count - 1
			With nodes.Item(i)
				Dim nodeText As String = String.Format("ISBN: {0}{1}Title: {2}{1}Author: {3}{1}", _
				 .Attributes("ISBN").Value, _
				 Environment.NewLine, _
				 .SelectSingleNode("title").InnerText, _
				 .SelectSingleNode("author").InnerText)
				MessageBox.Show(nodeText)
			End With
		Next
 
I see.

Unlike JavaScript, VB can (apparently) access collections directly (whereas I'm used to the Enumerator object to access them). That's probably what was the problem.

MattP said:
Here's where JohnH was trying to point you.

Pointing to doesn't really work for me :p
 
Back
Top