VB.NET: Get the Attribute and Text out from Xml string

  • Thread starter Thread starter http://www.visual-basic-data-mining.net/forum
  • Start date Start date
H

http://www.visual-basic-data-mining.net/forum

Besides using For Each /Next loop to find every individual nodes, which loop
should be use and how to apply if i want only one set of tag...(in blue)

Example:

<Book>
<Title ID ="1">
<Author>Hello</Author>
<Reference web = "www.reference.com" topic = "In thing">
</Title>
<Title ID = "2">
<Author>Thanks</Author>
<Reference web = "www.titlebook.com" topic = "Out thing">
</Title>
</Book>

I just want a draft idea of how it should be like...

Thanks
 
Just to clarify your question. . . .

Are you asking for code which will return a specific set of tags in this
document IE, the second set of tags between <Title> </Title> ?

I would have expected the xml to look more like this really. Is there any
reason why it is laid out the way you have it, or are you just playing with
some examples at the moment.

<Books>
<Book>
<Title ID ="1">
<Author>Hello</Author>
<Reference web = "www.reference.com" topic = "In thing">
</Title>
</Book>
<Book>
<Title ID = "2">
<Author>Thanks</Author>
<Reference web = "www.titlebook.com" topic = "Out thing">
</Title>
</Book>
</Books>



--

OHM ( Terry Burns )
. . . One-Handed-Man . . .
If U Need My Email ,Ask Me

Time flies when you don't know what you're doing
 
I would have expected the xml to look more like this really. Is there any
reason why it is laid out the way you have it, or are you just playing with

It's probably because whatever tool he used to post the message removed the
indenting. Although, I think the .xml is strange. Why is the Author tag
listed inside the Title tag? It seems that the Title should be an
attribute of Book:

<Books>
<Book ID="1" Title="Book Title Here">
<Author>Hello</Author>
<Reference web = "www.reference.com" topic = "In thing">
</Book>
<Book ID="2" Title="Second Book Title">
<Author>Thanks</Author>
<Reference web = "www.titlebook.com" topic = "Out thing">
</Book>
</Books>


Toytoy: As OHM stated, it is not clear what you want. Are you saying that
you need to get a particular <Book> node from your xml? Books by a certain
author? Books with a certain Topic?

Using the xml above:

'** BEGIN CODE HERE

Dim xDoc An New XmlDocument
xDoc.Load("books.xml")

'To select a list of books by a particular author:
Dim nodeList1 As XmlNodeList
Dim xNode As XmlNode
Dim root As XmlElement = xDoc.DocumentElement

'Get a list of books authored by Thanks
nodeList1 = root.SelectNodes("/Books/Book[Author=""Thanks""]")

'Get a book with a certain title:
xNode = root.SelectSingleNode("/Books/Book[@Title=""Book Title Here""]")

'Get books with a certain topic:
nodeList1 = root.SelectNodes("/Books/Book/Reference[@Topic=""In Thing""]")

'** END CODE


Hope this helps

--
Chris

dunawayc[AT]sbcglobal_lunchmeat_[DOT]net

To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
replace certain words in my E-Mail address.
 
Hi Chris.

Thanks for your reply.
It's probably because whatever tool he used to post the message removed the
indenting. Although, I think the .xml is strange. Why is the Author tag
listed inside the Title tag? It seems that the Title should be an
attribute of Book:

Like you, I was pointing out the Tag nesting not the indentation. He didnt
have a <Books> Element enclosing everything else which I found strange.

I'm not entirely sure what the OP is after and it would not suprise me if
they were perhaps a little confused regarding structure and formation. I
guess if they come back we might find out.

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .
If U Need My Email ,Ask Me

Time flies when you don't know what you're doing

Chris Dunaway said:
I would have expected the xml to look more like this really. Is there any
reason why it is laid out the way you have it, or are you just playing
with

<Books>
<Book ID="1" Title="Book Title Here">
<Author>Hello</Author>
<Reference web = "www.reference.com" topic = "In thing">
</Book>
<Book ID="2" Title="Second Book Title">
<Author>Thanks</Author>
<Reference web = "www.titlebook.com" topic = "Out thing">
</Book>
</Books>


Toytoy: As OHM stated, it is not clear what you want. Are you saying that
you need to get a particular <Book> node from your xml? Books by a certain
author? Books with a certain Topic?

Using the xml above:

'** BEGIN CODE HERE

Dim xDoc An New XmlDocument
xDoc.Load("books.xml")

'To select a list of books by a particular author:
Dim nodeList1 As XmlNodeList
Dim xNode As XmlNode
Dim root As XmlElement = xDoc.DocumentElement

'Get a list of books authored by Thanks
nodeList1 = root.SelectNodes("/Books/Book[Author=""Thanks""]")

'Get a book with a certain title:
xNode = root.SelectSingleNode("/Books/Book[@Title=""Book Title Here""]")

'Get books with a certain topic:
nodeList1 = root.SelectNodes("/Books/Book/Reference[@Topic=""In Thing""]")

'** END CODE


Hope this helps

--
Chris

dunawayc[AT]sbcglobal_lunchmeat_[DOT]net

To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
replace certain words in my E-Mail address.
 
yeah....

I am just wanted to simulate the question....so that you will understand..

I will be careful for what i post...

For what i know from others:

I must use XPath to search through the required ID so that it will return me
back the Elements and attributes for that particular ID.

But when i read through the XPath articles...I feel that it is some sort
like using SelectNodes("//Book/Title) to search and return certain nodes of
that string....

It cannot skip certain tag and retrieve other tags. ...Maybe is my concept
weak..
Say I want to display ID = "2" and web =www.titlebook.com...and skip Author
tag..

Example:



<Books>
<Book>
<Title ID ="1">
<Author>Hello</Author>
<Reference web = "www.reference.com" topic = "In thing">
</Title>
</Book>
<Book>
<Title ID = "2">
<Author>Thanks</Author>
<Reference web = "www.titlebook.com" topic = "Out thing">
</Title>
</Book>
</Books>

My coding:

Dim a, b As String
For Each n In xdoc.SelectNodes("//Title")
a = n.Attributes("ID").Value
textBox1.Text = a
Next

For Each n In xdoc.SelectNodes("//Title/Reference")
b = n.Attributes("web").Value
textbox2.Text = b
Next

Although it can still be retrieve but it retrieve all...but how to retrieve
mainly form ID = "2".... and also i can retrieve only in both tags in one
loop instead of all tags in two loop...

Lastly, do you have silimar articles with examples using XPath...

Thanks

--
toytoy - forum member
http://www.visual-basic-data-mining.net/forum


One Handed Man ( OHM - Terry Burns ) said:
Hi Chris.

Thanks for your reply.
It's probably because whatever tool he used to post the message removed the
indenting. Although, I think the .xml is strange. Why is the Author tag
listed inside the Title tag? It seems that the Title should be an
attribute of Book:

Like you, I was pointing out the Tag nesting not the indentation. He didnt
have a <Books> Element enclosing everything else which I found strange.

I'm not entirely sure what the OP is after and it would not suprise me if
they were perhaps a little confused regarding structure and formation. I
guess if they come back we might find out.

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .
If U Need My Email ,Ask Me

Time flies when you don't know what you're doing

Chris Dunaway said:
I would have expected the xml to look more like this really. Is there any
reason why it is laid out the way you have it, or are you just playing
with

<Books>
<Book ID="1" Title="Book Title Here">
<Author>Hello</Author>
<Reference web = "www.reference.com" topic = "In thing">
</Book>
<Book ID="2" Title="Second Book Title">
<Author>Thanks</Author>
<Reference web = "www.titlebook.com" topic = "Out thing">
</Book>
</Books>


Toytoy: As OHM stated, it is not clear what you want. Are you saying that
you need to get a particular <Book> node from your xml? Books by a certain
author? Books with a certain Topic?

Using the xml above:

'** BEGIN CODE HERE

Dim xDoc An New XmlDocument
xDoc.Load("books.xml")

'To select a list of books by a particular author:
Dim nodeList1 As XmlNodeList
Dim xNode As XmlNode
Dim root As XmlElement = xDoc.DocumentElement

'Get a list of books authored by Thanks
nodeList1 = root.SelectNodes("/Books/Book[Author=""Thanks""]")

'Get a book with a certain title:
xNode = root.SelectSingleNode("/Books/Book[@Title=""Book Title Here""]")

'Get books with a certain topic:
nodeList1 = root.SelectNodes("/Books/Book/Reference[@Topic=""In Thing""]")

'** END CODE


Hope this helps

--
Chris

dunawayc[AT]sbcglobal_lunchmeat_[DOT]net

To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
replace certain words in my E-Mail address.
 
Back
Top