473,405 Members | 2,444 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,405 software developers and data experts.

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

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

2 1952
Frinavale
9,735 Expert Mod 8TB
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
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

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

Similar topics

1
by: Igor | last post by:
Is there any way to resort and xml document using xslt based on element position. For example if I have xml like this: <root> <element> 1st thing </element> <element> 2nd thing </element>...
4
by: Michael Hill | last post by:
If I had a span tag like: <span id='msg_2'> There is a bunch of text <a href="http://www.someurl.com"> here </a>. And some others <a href="http://www.otherurl.com">here</a>. Good luck! </span>...
3
by: abs | last post by:
Has anybody seen a function which checked if one of passed elements is nested in the node of another element passed to function, no matter how deep it is nested ? Thanks in advance, ABS
0
by: eva.mukhija | last post by:
I have a follwing xml structure <ParentNode> <ChildeNode1>value1</ChildNode1> <ChildeNode2>value2</ChildNode2> <ChildeNode3>value3</ChildNode3> </ParentNode> The xml is verly large to parse and...
4
by: David Krmpotic | last post by:
Hello, Can please tell me how to do it ? This should be simple, because I'd think it's used a lot.. but in reality is not that simple and there is at least 50 topics about it in newsgroups,...
1
by: ronchese | last post by:
Hello All. I need to complement some information in a xml file, using the Xml.XmlDocument object. This xml contains several <xs:element ....> nodes (is a saved dataset), and I need to write new...
3
by: jm.suresh | last post by:
Hi, I have a list and I want to find the first element that meets a condition. I do not want to use 'filter', because I want to come out of the iteration as soon as the first element is found. I...
0
by: twingate | last post by:
Hi all, I'm looking to develop an application where I'd like a node-based graph type interface. There are several applications that have similar interfaces for various functionality; video...
4
by: mab464 | last post by:
I have this code on my WAMP server running on my XP machine if ( isset( $_POST ) ) { for($i=0; $i<count($_POST);$i++) { if ($ans != NULL ) $ans .= ", " . $_POST ; // Not the first...
1
by: farzi | last post by:
how can I transform an attribute text node to a element tag , for example I have: <objectClass> <objectClass name="CAR"> </objectClass> which I want to transform it to the following:
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: 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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.