By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,347 Members | 2,120 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,347 IT Pros & Developers. It's quick & easy.

VB.Net XML Retrieve entire node based on first element.

P: 1
Example
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="UTF-8"?>
  2.  <BooksXML>
  3.    <Books>
  4.  
  5.      <BookInfo>
  6.         <BookName>Hobbit</BookName>
  7.         <Author>Token</Author>
  8.         <Genre>Fantasy</Genre>
  9.      </BookInfo>
  10.  
  11.      <BookInfo>
  12.         <BookName>Spirits Of Lore</BookName>
  13.         <Author>Yolenda</Author>
  14.         <Genre>Romance</Genre>
  15.      </BookInfo>
  16.  
  17.    </Books>
  18. </BooksXML>
I need to return the whole node using with a query criteria of "Hobbit" so it would return
Hobbit
Token
Fantasy

I've tried
Expand|Select|Wrap|Line Numbers
  1. Dim BookName As String = cb_BookList.SelectedItem
  2. Dim xmlDoc As New XmlDocument()
  3. xmlDoc.Load(BooksXMLLocation)
  4. Dim nodes As XmlNodeList = xmlDoc.DocumentElement.SelectNodes("/BooksXML/Books/BookInfo")
  5.  
  6. Dim BookName As String = ""
  7. Dim BookAuther As String = "" 
  8. Dim Genre As String = ""
  9. For Each node As XmlNode In nodes
  10.    BookName = node.SelectSingleNode("BookName").InnerText
  11.    BookAuther =node.SelectSingleNode("BookAuther").InnerText
  12.    Genre = node.SelectSingleNode("Genre").InnerText
  13. Next
Any help would be appreciated
Thank you
Jan 21 '15 #1

✓ answered by Frinavale

It looks like you stopped half way to what you want to accomplish.

It looks like you should successfully be grabbing all of the BookInfo nodes and that you are looping through them.

The only thing you seem to be missing is an If statement to find the information you are looking for.


Expand|Select|Wrap|Line Numbers
  1.     Dim bookNameToSerachFor As String = cb_BookList.SelectedItem
  2.     Dim xmlDoc As New XmlDocument()
  3.     xmlDoc.Load(BooksXMLLocation)
  4.     Dim nodes As XmlNodeList = xmlDoc.DocumentElement.GetElementsByTagName("BookInfo")
  5.  
  6.     Dim bookName As String = ""
  7.     Dim bookAuthor As String = "" 
  8.     Dim genre As String = ""
  9.     For Each node As XmlNode In nodes
  10.        bookName = node.SelectSingleNode("BookName").InnerText
  11.        If String.Compare(bookNameToSearchFor, bookName, True) = 0 Then
  12.          bookName = node.SelectSingleNode("BookName").InnerText
  13.          bookAuthor =node.SelectSingleNode("Author").InnerText
  14.          genre = node.SelectSingleNode("Genre").InnerText
  15.          Exit For
  16.        End If
  17.     Next
  18. 'At this point you have the information 
  19. 'about the book that you were searching for...
  20. 'Or you don't because you couldn't find the book

Mind you maybe you are having a problem because you misspelled the word Author (you spelled it as Auther). This would mean that you would never find a the node containing the author information.

Actually there is no node called "BookAuthor" either!
You need to ensure that you use the appropriate names when you are retrieving nodes. You should be using "Author".

In the future you need to state more details about what is not working.


-Frinny

Share this Question
Share on Google+
2 Replies


Frinavale
Expert Mod 5K+
P: 9,731
It looks like you stopped half way to what you want to accomplish.

It looks like you should successfully be grabbing all of the BookInfo nodes and that you are looping through them.

The only thing you seem to be missing is an If statement to find the information you are looking for.


Expand|Select|Wrap|Line Numbers
  1.     Dim bookNameToSerachFor As String = cb_BookList.SelectedItem
  2.     Dim xmlDoc As New XmlDocument()
  3.     xmlDoc.Load(BooksXMLLocation)
  4.     Dim nodes As XmlNodeList = xmlDoc.DocumentElement.GetElementsByTagName("BookInfo")
  5.  
  6.     Dim bookName As String = ""
  7.     Dim bookAuthor As String = "" 
  8.     Dim genre As String = ""
  9.     For Each node As XmlNode In nodes
  10.        bookName = node.SelectSingleNode("BookName").InnerText
  11.        If String.Compare(bookNameToSearchFor, bookName, True) = 0 Then
  12.          bookName = node.SelectSingleNode("BookName").InnerText
  13.          bookAuthor =node.SelectSingleNode("Author").InnerText
  14.          genre = node.SelectSingleNode("Genre").InnerText
  15.          Exit For
  16.        End If
  17.     Next
  18. 'At this point you have the information 
  19. 'about the book that you were searching for...
  20. 'Or you don't because you couldn't find the book

Mind you maybe you are having a problem because you misspelled the word Author (you spelled it as Auther). This would mean that you would never find a the node containing the author information.

Actually there is no node called "BookAuthor" either!
You need to ensure that you use the appropriate names when you are retrieving nodes. You should be using "Author".

In the future you need to state more details about what is not working.


-Frinny
Jan 23 '15 #2

P: 4
Dear Friend,
I have made your project(as you wanted).I have uploaded the files to the following URL:-

http://www.filehosting.org/file/details/478406/Rex.rar

This RAR file has three files inside.One .xml and two .vb files.I have used the code by Frinavale-but made some changes(spelling correction and a
one or two minor additions).These files have to be processed by C-Pad technique(Developed by me)-details see URL:-

http://www.dreamincode.net/forums/to...&#entry2124851

This C-pad technique hold good for VB.Net and WPF/VB.Net applications.Here you just call the compiler and msbuild from your cmd window.
It works very fine.Gives light weight results.No unnecessary extra baggages(files).
You have to do two things before compiling:-
1.Correct the path given in Rex22.vb file-from "C:\Users\VEKATRAMAN\Desktop\Rex\BooksXMLLocation. xml" to

"C:\Users\......\Desktop\Rex\BooksXMLLocation. xml"

Here the dotted portion is particular to your computer.Propably your name.Keep all three files in desktop in Folder "Rex".
Now the compiler calling is as follows:-

path=%path%;c:\windows\microsoft.net\framework\v4. 0.30319
VBC /OUT:Rex1 /T:winexe C:\Users\........\Desktop\Rex\Rex1.vb
VBC /OUT:Rex1 /T:winexe C:\Users\........\Desktop\Rex\Rex11.vb C:\Users\VEKATRAMAN\Desktop\Rex\Rex1.vb
Rex1

Your executive file will be in C:\Users\........ folder.All VB.Net (by C-Pad) .exe files get created here only.

Regards and blessings,
Happy coding,

Venkatraman/kvinvisibleguy
(age-68 +)
Mar 22 '15 #3

Post your reply

Sign in to post your reply or Sign up for a free account.