VB.NET: Get the Attribute and Text out from Xml....[Resolved]
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)
more sugguestions and related articles will be greatly appreciate...
Thanks
VB.NET: Get the Attribute and Text out from Xml....[Resolved]
Say i have these Xml tag
Code:
<Books>
<Title section ="1">
<Reference web = "www.any.com" topic = "1">
</Title>
<Title section ="1">
<Reference web = "www.idea.com" topic = "2">
</Title>
<Title section ="2">
<Reference web = "www.howTo.com" topic = "3">
</Title>
<Title section ="3">
<Reference web = "www.do.com" topic = "4">
</Title>
</Books>
I want to extract all the web and topic attribute only in Title section "1"...
Thanks
Re: VB.NET: Get the Attribute and Text out from Xml....[Unresolved]
Did you check out the example code I posted in the zip file?
It shows you how to do that. The only difference would be referring to the topic attribute exactly the way the example does the web attribute.
Re: VB.NET: Get the Attribute and Text out from Xml....[Unresolved]
yeah....cannot..
Once i selected ID "1" which is the same with two title data..
it display only the first data of web and topics since two have the same ID..
Say how to extract all web and topic involve same and different ID into the combo box ..
Thanks..
Re: VB.NET: Get the Attribute and Text out from Xml....[Unresolved]
Then instead of calling SelectSingleNode use SelectNodes and loop through the results.
Re: VB.NET: Get the Attribute and Text out from Xml....[Unresolved]
From the previous Xml tags:
Code:
<Books>
<Title section ="1">
<Reference web = "www.any.com" topic = "1">
</Title>
<Title section ="5">
<Reference web = "www.idea.com" topic = "2">
</Title>
<Title section ="5">
<Reference web = "www.howTo.com" topic = "3">
</Title>
<Title section ="9">
<Reference web = "www.do.com" topic = "4">
</Title>
</Books>
Say i have 3 section name for the Title:
"1" stand for Science, "2" stand for Gerenal and "3" stand for History
when i receive the section attribute from the server, i have to do some comparing with the number...like 2 to the power of 1 indicate the event of Science, 2 to the power of 2 indicate Gerenal and so on.
Thus when i receive the number "5" for section in this case, it indicate the events of Science and Gerenal...
so all those fields with attribute "5" will be extract.......
The exponent method is:
Code:
Public Function pow(ByVal exponent As Integer, ByVal base As Integer)
result = base ^ exponent
subtotal = result
total = total + subtotal
Return total
End Function
with the above method, what should i do to compare the number with the section attributes correctly and display it to a listbox...
Currently, i may extract the web and topic.... but it will duplicate the values whenever i extract from it...
Example: listbox should contain:
www.howTo.com
3
www.idea.com
2
But my coding gave me this:
www.howTo.com
3
www.idea.com
2
www.howTo.com
3
www.idea.com
2
www.howTo.com
3
www.idea.com
2
www.howTo.com
3
www.idea.com
2
See, when i extract two sets of data, it will duplicate four sets instead...
Thanks
Re: VB.NET: Get the Attribute and Text out from Xml....[Unresolved]
According to the above XML tags...
I want to extract all attributes and display accordingly...
I will always have duplicate values...
That means...my looping is wrong..
Any ideas how to do a simple and accurate compare loop and extract the required attributes from all the loop into different listbox...
Thanks
Re: VB.NET: Get the Attribute and Text out from Xml....[Unresolved]
Post your code and I'll help you fix it.
Re: VB.NET: Get the Attribute and Text out from Xml....[Unresolved]
Say when I press a button, the server will send the Xml tag as below..
Code:
<Books>
<Title section ="1">
<Reference web = "www.any.com" topic = "1">
</Title>
<Title section ="5">
<Reference web = "www.idea.com" topic = "2">
</Title>
<Title section ="5">
<Reference web = "www.howTo.com" topic = "3">
</Title>
<Title section ="9">
<Reference web = "www.do.com" topic = "4">
</Title>
</Books>
The first thing i did was to pass the whole tag to this sortXml method
whereby it will extract the section out to compare...
Code:
Public Function sortXml(ByVal wholeXml As String) As String
Dim no As String
Dim xdoc As New XmlDocument
Try
xdoc.LoadXml(wholeXml)
For Each nod As XmlNode In xdoc.SelectNodes("//Books/Title")
no = nod.Attributes("section").Value
sortSection(no, wholeXml) ' then i pass here to check the exponent
Next
Catch ex As Exception
End Try
End Function
Since i must compare the exponent so i declare the constant and set some flags to false
Code:
Const mintSCIENCE As Integer = 0
Const mintGENERAL As Integer = 1
Const mintCOMPUTER As Integer = 2
Dim blnScience, blnGeneral, blnComputer As boolean
The code inside sortSection is:
Code:
Public Function sortSection(ByVal sect As Integer, ByVal wholeXml2 As String)
blnScience = False
blnGeneral = False
blnComputer = False
Dim strScience, strGerenal, strComputer, output as String
Dim xdoc As New XmlDocument
xdoc.LoadXml(wholeXml2)
output = sect
If output = pow(mintSCIENCE, 2) Or output > pow(mintSCIENCE,2) Then
blnScience = True
output = output - pow(mintSCIENCE, 2)
strScience = "Science" ' I will pass and compare this string
populateData(strScience, wholeXml2) ' inside the populateData method
End If
If output = pow(mintGENERAL, 2) Or output > pow(mintGENERAL, 2) Then
blnGeneral = True
output = output - pow(mintGENERAL, 2)
strGeneral = "General"
populateData(strGeneral, wholeXml2)
End If
If output = pow(mintCOMPUTER, 2) Or output > pow(mintCOMPUTER, 2) Then
blnComputer = True
output = output - pow(mintCOMPUTER, 2)
strComputer = "Computer"
populateData(strComputer, wholeXml2)
End If
End function
The codes for pow and populateData are:
Code:
Public Function pow(ByVal exponent As Integer, ByVal base As Integer)
result = 1 'Assume all the values are declared
Dim i As Integer
For i = 0 To exponent - 1 Step i + 1
result = result * base
Next
Return result
End Function
Public Sub populateData(ByVal val As String, ByVal wholeXml3 As String)
Dim str1, str2 As String
Dim xdoc As New XmlDocument
xdoc.LoadXml(wholeXml3)
If val = "Science" Then
For Each nod As XmlNode In xdoc.SelectNodes("//Books/Title/Reference")
str1 = nod.Attributes("web").Value
str2 = nod.Attributes("topic").Value
lst1.Items.Add(str1)
lst1.Items.Add(str2)
Next
End If
If val = "General" Then
For Each nod As XmlNode In xdoc.SelectNodes("//Books/Title/Reference")
str1 = nod.Attributes("web").Value
str2 = nod.Attributes("topic").Value
lst2.Items.Add(str1)
lst2.Items.Add(str2)
Next
End If
If val = "Computer" Then
For Each nod As XmlNode In xdoc.SelectNodes("//Books/Title/Reference")
str1 = nod.Attributes("web").Value
str2 = nod.Attributes("topic").Value
lst3.Items.Add(str1)
lst3.Items.Add(str2)
Next
End If
I have a lot of logic error which i can't figure out how to solve when it involve exponent to compare and populate the data into the listboxes...
Thanks
Re: VB.NET: Get the Attribute and Text out from Xml....[Unresolved]
Does anyone know how to solve....
Thanks
Re: VB.NET: Get the Attribute and Text out from Xml
Finally item will not duplicate.....
Code:
Public Function sortXml(ByVal wholeXml As String) As String
Dim no, web2, topics2 As String
Dim xdoc As New XmlDocument
Dim nod2 As XmlNode
Try
xdoc.LoadXml(wholeXml)
For Each nod As XmlNode In xdoc.SelectNodes("//Books/Title")
no = nod.Attributes("section").Value
nod2 = nod.FirstChild()
web2 = nod2.Attributes("web").Value
topics2 = nod2.Attributes("topic").Value
sortSection(no, web2, topics2) ' then i pass here to check the exponent
Next
Catch ex As Exception
End Try
End Function
Since i must compare the exponent so i declare the constant and set some flags to false
Code:
Const mintSCIENCE As Integer = 0
Const mintGENERAL As Integer = 1
Const mintCOMPUTER As Integer = 2
Dim blnScience, blnGeneral, blnComputer As boolean
The code inside sortSection is:
Code:
Public Function sortSection(ByVal sect As Integer, ByVal web3 As String, ByVal topics3 As String )
blnScience = False
blnGeneral = False
blnComputer = False
Dim strScience, strGerenal, strComputer, output as String
output = sect
If output = pow(mintSCIENCE, 2) Or output > pow(mintSCIENCE,2) Then
blnScience = True
output = output - pow(mintSCIENCE, 2)
strScience = "Science" ' I will pass and compare this string
populateData(strScience, web3, topics3) ' inside the populateData method
End If
If output = pow(mintGENERAL, 2) Or output > pow(mintGENERAL, 2) Then
blnGeneral = True
output = output - pow(mintGENERAL, 2)
strGeneral = "General"
populateData(strGeneral, web3, topics3)
End If
If output = pow(mintCOMPUTER, 2) Or output > pow(mintCOMPUTER, 2) Then
blnComputer = True
output = output - pow(mintCOMPUTER, 2)
strComputer = "Computer"
populateData(strComputer, web3, topics3)
End If
End function
The codes for pow and populateData are:
Code:
Public Function pow(ByVal exponent As Integer, ByVal base As Integer)
result = 1 'Assume all the values are declared
Dim i As Integer
For i = 0 To exponent - 1 Step i + 1
result = result * base
Next
Return result
End Function
Public Sub populateData(ByVal val As String, ByVal web4 As String, ByVal topics4 As String)
If val = "Science" Then
lst1.Items.Add(web4)
lst1.Items.Add(topics4)
End If
If val = "General" Then
lst2.Items.Add(web4)
lst2.Items.Add(topics4)
End If
If val = "Computer" Then
lst3.Items.Add(web4)
lst3.Items.Add(topics4)
End If
End sub
:) :) :) :) :)