Click to See Complete Forum and Search --> : VB.NET: Get the Attribute and Text out from Xml string
toytoy
September 27th, 2004, 04:49 AM
Say i receive this string from the server:
s = "<Main Type="Personal">Title</Main>"
How to extract the attributes = "Personal" from Main tag
and the Text ="Title" using LoadXml(s)
Thanks
GungaDin
September 27th, 2004, 07:38 PM
Give this a try:
' Load up the XML file
Dim xmlDoc As New XmlDocument
xmlDoc.LoadXml("<Main Type='Personal'>Title</Main>")
Dim n As XmlNode
' Scroll through the nodes
For Each n In xmlDoc.SelectNodes("Main")
Debug.WriteLine(n.Attributes("Type").Value)
Debug.WriteLine(n.InnerText)
Next
Hope this helps,
Nathan.
toytoy
September 27th, 2004, 08:54 PM
Thanks...it works
How to create a loop to see all InnerText and Attributes...
What if i want to select only either all the attributes or InnerText to view....
Example:
LoadXml("<Main Type='Personal'><Time Hour="23" Min = "50"> Appointmnet</Time><Time Hour="10" Min = "40">Booking</Time></Main>)"
Any articles or examples to go about it...
Thanks
GungaDin
September 27th, 2004, 09:05 PM
Well, the code I sent through does actually loop through all the nodes:
' Load up the XML file
Dim xmlDoc As New XmlDocument
xmlDoc.LoadXml("<root><Main Type='Personal'>Title</Main><Main Type='Relative'>Sub</Main></root>")
Dim n As XmlNode
' Scroll through the nodes
For Each n In xmlDoc.SelectNodes("root/Main")
Debug.WriteLine(n.Attributes("Type").Value)
Debug.WriteLine(n.InnerText)
Next
Hope this helps,
Nathan.
toytoy
September 27th, 2004, 09:11 PM
Sorry..
As i rewrite the code, you at the same time send me the solution..
How about the above edit example
I will try your given examples..
Thanks a lot
toytoy
September 30th, 2004, 03:26 AM
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...
( all attribute and element in ID =2 for the Title tag)
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...
GungaDin
September 30th, 2004, 05:51 PM
Ahhhh, gotcha.
OK, firstly, your syntax for the XML file isn't quite correct. It should be more like this:
<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>
You missed out the / on the end of your reference line.
Now, the code you want is something like this I think:
Dim xmlDoc As New XmlDocument
xmlDoc.LoadXml("<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>")
' This will get the node with an ID = 2 in the Title field
Dim n As XmlNode = xmlDoc.SelectSingleNode("Book/Title[@ID='2']")
' From here you can get your data this way
Debug.WriteLine("Author: " & n.SelectSingleNode("Author").InnerText)
Debug.WriteLine("Reference web: " & n.SelectSingleNode("Reference").Attributes("web").Value)
Hope this helps,
Nathan.
toytoy
September 30th, 2004, 09:30 PM
what if there is 100 of IDs which i did not know, is that means that i have to create 100 of that loop..
And i read from some forums website..
TextBox2.Text=xdoc.SelectSingleNode("//themes/@value").Value
TextBox3.Text=xdoc.SelectSingleNode("//locationupdate/@path").Value
What is the @ represent in the above coding... and how it works..
thanks...
GungaDin
September 30th, 2004, 11:03 PM
OK, not sure what you're after now. I'll try and answer what I think you're asking.
If you want to loop through all the nodes:
Dim nl As XmlNodeList = xmlDoc.SelectNodes("/Book/Title")
Dim n As XmlNode
For Each n In nl
Debug.WriteLine("Author: " & n.SelectSingleNode("Author").InnerText)
Debug.WriteLine("Reference web: " & n.SelectSingleNode("Reference").Attributes("web").Value)
Next
If you just want to list all the web attributes for the Reference nodes, you could do something like:
Dim nl As XmlNodeList = xmlDoc.SelectNodes("/Book/Title/Reference/@web")
Dim n As XmlNode
For Each n In nl
Debug.WriteLine("Reference web: " & n.InnerText)
Next
If you know the ID and you want to get all the child nodes and their values and attributes:
Dim nl As XmlNodeList = xmlDoc.SelectSingleNode("Book/Title[@ID='2']").ChildNodes
Dim n As XmlNode
Dim a As XmlAttribute
For Each n In nl
Debug.WriteLine(n.Name & " - " & n.InnerText)
For Each a In n.Attributes
Debug.WriteLine(" " & a.Name & " - " & a.Value)
Next
Next
If you know the ID you're after and you want to get an attribute:
xmlDoc.SelectSingleNode("/Book/Title[@ID='2']/Reference/@web").Value
The @ refers to an attribute.
Does any of that answer your question?
toytoy
October 1st, 2004, 12:14 AM
no...
What i means if all the ID is not known....until runtime
How to you put in in your code....if i retrieve 100 ID..
that means i have to code
Dim nl As XmlNodeList = xmlDoc.SelectSingleNode("Book/Title[@ID='2']").ChildNodes
'do something------------------------------------------------
Dim nl As XmlNodeList = xmlDoc.SelectSingleNode("Book/Title[@ID='3']").ChildNodes
'do something-----------------------------------------------
-----------------------------------------------------------------
-----------------------------------------------------------------
Dim nl As XmlNodeList = xmlDoc.SelectSingleNode("Book/Title[@ID='100']").ChildNodes
'do something------------------------------------------------
is it a waste of time...
GungaDin
October 1st, 2004, 12:26 AM
No, if you don't know the ID's and you want to go through all the children:
Dim nl As XmlNodeList = xmlDoc.SelectNodes("/Book/Title")
Dim n, pn As XmlNode
Dim a As XmlAttribute
For Each pn In nl
Debug.WriteLine("Title ID: " & pn.Attributes("ID").Value)
For Each n In pn.ChildNodes
Debug.WriteLine(n.Name & " - " & n.InnerText)
For Each a In n.Attributes
Debug.WriteLine(" " & a.Name & " - " & a.Value)
Next
Next
Next
Is that what you're after?
toytoy
October 25th, 2004, 11:07 AM
Say I have this Xml taken as an example:
<Settings>
<Connection>
<UserID>sa</UserID>
<DataSource>Server</DataSource>
<InitialCatalog>Database</InitialCatalog>
</Connection>
</Settings>
To get the Xml text is:
Dim m_xmld As XmlDocument
Dim m_nodelist As XmlNodeList
Dim m_node As XmlNode
m_xmld = New XmlDocument()
m_xmld.Load("Settings.xml")
m_nodelist = m_xmld.SelectNodes("/Settings/Connection")
For Each m_node In m_nodelist
Dim UserID = m_node.ChildNodes.Item(0).InnerText
Dim DataSource = m_node.ChildNodes.Item(1).InnerText
Dim InitialCatalog = m_node.ChildNodes.Item(2).InnerText
Next
How about if i add in one time tag...
<Settings>
<Connection>
<UserID>sa</UserID>
<DataSource>Server</DataSource>
<InitialCatalog>Database</InitialCatalog>
<Register>
<Time hour="23" min=40">
<Date>22/10/78</Date>
</Register>
</Connection>
<Connection>
<UserID>sa</UserID>
<DataSource>Server</DataSource>
<InitialCatalog>Database</InitialCatalog>
<Register>
<Time hour="23" min=40">
<Date>22/10/78</Date>
</Register>
</Connection>
</Settings>
How do i use the above node.ChildNodes method to get the "23" or "40" out from the Xml tag....
I need to loop through all the nodes...and I am using XmlDocument...
Any suggestion or link will be appreciated...
Thanks
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.