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

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

P: n/a
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

--
toytoy - forum member
http://www.visual-basic-data-mining.net/forum
Nov 21 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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

"http://www.visual-basic-data-mining.net/forum" <si******@gmail.com> wrote
in message news:%2******************@TK2MSFTNGP12.phx.gbl...
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

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

Nov 21 '05 #2

P: n/a
On Thu, 30 Sep 2004 11:27:02 +0100, One Handed Man ( OHM - Terry Burns )
wrote:
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.
Nov 21 '05 #3

P: n/a
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" <"dunawayc[[at]_lunchmeat_sbcglobal[dot]]net"> wrote in
message news:1s*****************************@40tude.net... On Thu, 30 Sep 2004 11:27:02 +0100, One Handed Man ( OHM - Terry Burns )
wrote:
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.

Nov 21 '05 #4

P: n/a
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 )" <news.microsoft.com> wrote in message
news:u5**************@TK2MSFTNGP11.phx.gbl...
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" <"dunawayc[[at]_lunchmeat_sbcglobal[dot]]net"> wrote in
message news:1s*****************************@40tude.net...
On Thu, 30 Sep 2004 11:27:02 +0100, One Handed Man ( OHM - Terry Burns )
wrote:
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.


Nov 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.