To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
VBForums  

VB Wire News
MSDN Subscribers: Download the VS 2010 Release Candidate
MSDN Subscribers: Download the VS 2010 Release Candidate
Sell Your Code and Make Money?
Creating your own Tetris game using VB.NET
Article :: Improving Software Economics, Part 4 of 7: Top 10 Principles of Iterative Software Management



Go Back   VBForums > Visual Basic > Visual Basic .NET

Reply Post New Thread
 
Thread Tools Search this Thread Display Modes
Old Sep 27th, 2004, 05:57 AM   #1
toytoy
Addicted Member
 
toytoy's Avatar
 
Join Date: Jul 04
Posts: 230
toytoy is an unknown quantity at this point (<10)
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

Last edited by toytoy; Jan 22nd, 2005 at 12:06 AM.
toytoy is offline   Reply With Quote
Old Sep 27th, 2004, 01:56 PM   #2
Edneeis
Your Ad Here!
 
Edneeis's Avatar
 
Join Date: Feb 00
Location: Moreno Valley, CA (SoCal)
Posts: 7,341
Edneeis is a jewel in the rough (300+)Edneeis is a jewel in the rough (300+)Edneeis is a jewel in the rough (300+)Edneeis is a jewel in the rough (300+)
See if this helps:

http://www.vbforums.com/showthread.p...hreadid=253267

There are other previous threads on this topic too if you want to try and search the forums.
Edneeis is offline   Reply With Quote
Old Sep 28th, 2004, 12:42 AM   #3
toytoy
Addicted Member
 
toytoy's Avatar
 
Join Date: Jul 04
Posts: 230
toytoy is an unknown quantity at this point (<10)
Thanks...

I have seem the examples..

Besides being able to select any attribute and Elements to display, how to actually retrieve all the elements and attributes..
Say i want to display all in various textboxes at one go ...

Example of the string: (Taken form the thread given)
Code:
<chimaera>
<servers>
<host ip="172.16.1.30">Chimaera</host>
</servers>
<themes value="true" />
<locationupdate path="C:\1.xml" />
</chimaera>
Which mean the various textboxes should contain:
Code:
TextBox1.Text =  172.16.1.30
TextBox2.Text = Chimaera
TextBox3.Text = true
TextBox4.Text = (All the contents in 1.xml)
Thanks

Last edited by toytoy; Dec 3rd, 2004 at 08:03 AM.
toytoy is offline   Reply With Quote
Old Sep 28th, 2004, 02:34 AM   #4
Edneeis
Your Ad Here!
 
Edneeis's Avatar
 
Join Date: Feb 00
Location: Moreno Valley, CA (SoCal)
Posts: 7,341
Edneeis is a jewel in the rough (300+)Edneeis is a jewel in the rough (300+)Edneeis is a jewel in the rough (300+)Edneeis is a jewel in the rough (300+)
Just replace the code parts that use a Msgbox with the same values into Textboxes. To get the value 'Chimaera' you would refer to the text property of the node itself instead of an attribute but the rest should be the same.
Edneeis is offline   Reply With Quote
Old Sep 28th, 2004, 03:04 AM   #5
toytoy
Addicted Member
 
toytoy's Avatar
 
Join Date: Jul 04
Posts: 230
toytoy is an unknown quantity at this point (<10)
ok....Thanks

toytoy is offline   Reply With Quote
Old Sep 29th, 2004, 10:20 PM   #6
toytoy
Addicted Member
 
toytoy's Avatar
 
Join Date: Jul 04
Posts: 230
toytoy is an unknown quantity at this point (<10)
Another problem...

Say i have these Xml tag..
Code:
<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>
And the method that i use is XmlDocument..

If the user key in the text in the textbox...how to tell the XmlDocument that the text refer to certain ID..

And how to display all the attributes and Elements of that ID to all the textbox.....

Thanks

Last edited by toytoy; Dec 3rd, 2004 at 08:03 AM.
toytoy is offline   Reply With Quote
Old Sep 29th, 2004, 11:01 PM   #7
Edneeis
Your Ad Here!
 
Edneeis's Avatar
 
Join Date: Feb 00
Location: Moreno Valley, CA (SoCal)
Posts: 7,341
Edneeis is a jewel in the rough (300+)Edneeis is a jewel in the rough (300+)Edneeis is a jewel in the rough (300+)Edneeis is a jewel in the rough (300+)
In the previous example you notice later on I passed a longer string to the SelectNodes method. This is called an XPath or XQuery and you can query XML using this. To find a specific title by id you would use an XPath something like this:

VB Code:
  1. For Each nod As XmlNode In xdoc.SelectNodes("//Book/Title[@ID=1]") 'select title node with id=1
  2.             'handle different parts of the Title node here
  3.         Next

http://www.w3.org/TR/xpath
Edneeis is offline   Reply With Quote
Old Sep 30th, 2004, 01:12 AM   #8
toytoy
Addicted Member
 
toytoy's Avatar
 
Join Date: Jul 04
Posts: 230
toytoy is an unknown quantity at this point (<10)
Just to ask how this MsgBox part can be replace by string...
Code:
Dim xdoc As New XmlDocument
xdoc.Load("..\data.xml")

For Each nod As XmlNode In xdoc.SelectNodes("//servers/host")
     MsgBox(nod.Attributes("ip").Value, , "host")
Next

MsgBox(xdoc.SelectSingleNode("//themes/@value").Value, , "themes")
MsgBox(xdoc.SelectSingleNode("//locationupdate/@path").Value, , "locationupdate")
Thanks

Last edited by toytoy; Dec 3rd, 2004 at 08:04 AM.
toytoy is offline   Reply With Quote
Old Sep 30th, 2004, 01:37 AM   #9
Edneeis
Your Ad Here!
 
Edneeis's Avatar
 
Join Date: Feb 00
Location: Moreno Valley, CA (SoCal)
Posts: 7,341
Edneeis is a jewel in the rough (300+)Edneeis is a jewel in the rough (300+)Edneeis is a jewel in the rough (300+)Edneeis is a jewel in the rough (300+)
VB Code:
  1. Dim xdoc As New XmlDocument
  2. xdoc.Load("..\data.xml")
  3. For Each nod As XmlNode In xdoc.SelectNodes("//servers/host")
  4.     TextBox1.Text &=nod.Attributes("ip").Value
  5. Next
  6. TextBox2.Text=xdoc.SelectSingleNode("//themes/@value").Value
  7. TextBox3.Text=xdoc.SelectSingleNode("//locationupdate/@path").Value

But really you should add some error handling just in case no matching node is found.
VB Code:
  1. Dim xdoc As New XmlDocument
  2. xdoc.Load("..\data.xml")
  3. For Each nod As XmlNode In xdoc.SelectNodes("//servers/host")
  4.     If Not nod Is Nothing Then TextBox1.Text &=nod.Attributes("ip").Value
  5. Next
  6. Dim nod As XmlNode
  7. node=xdoc.SelectSingleNode("//themes/@value")
  8. If Not nod Is Nothing Then TextBox2.Text=nod.Value
  9. node=xdoc.SelectSingleNode("//locationupdate/@path")
  10. If Not nod Is Nothing Then TextBox3.Text=nod.Value
Edneeis is offline   Reply With Quote
Old Sep 30th, 2004, 02:19 AM   #10
toytoy
Addicted Member
 
toytoy's Avatar
 
Join Date: Jul 04
Posts: 230
toytoy is an unknown quantity at this point (<10)
ok....thanks

if you all guys have found any useful threads in this site or any useful articles besides the above link. .....

feel free to put the link here...

any help will be appreciated....

toytoy is offline   Reply With Quote
Old Sep 30th, 2004, 02:30 AM   #11
toytoy
Addicted Member
 
toytoy's Avatar
 
Join Date: Jul 04
Posts: 230
toytoy is an unknown quantity at this point (<10)
one more question...

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:
Code:
<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...

Last edited by toytoy; Dec 3rd, 2004 at 08:04 AM.
toytoy is offline   Reply With Quote
Old Sep 30th, 2004, 09:49 PM   #12
toytoy
Addicted Member
 
toytoy's Avatar
 
Join Date: Jul 04
Posts: 230
toytoy is an unknown quantity at this point (<10)
For what i know:

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:
Code:
<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>    'skip this part    
<Reference web = "www.titlebook.com" topic = "Out thing">
</Title>
</Book>
</Books>
My coding:
Code:
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".... instead of retrieve all the two tags using two loops, how to use it in one loop...

And in the last two thread that Edneeis send....I feel that it cannot be done since the program does not know which ID until the user key in something inside the textbox....what if there is 100 of IDs which i did not know, is that means that i have to create 100 of that loop..
Code:
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..

Lastly, do you have silimar articles with examples using XPath...
XPath expert, i need your help in clearing my unclear concepts..

Thanks

Last edited by toytoy; Dec 3rd, 2004 at 08:05 AM.
toytoy is offline   Reply With Quote
Old Sep 30th, 2004, 11:09 PM   #13
Edneeis
Your Ad Here!
 
Edneeis's Avatar
 
Join Date: Feb 00
Location: Moreno Valley, CA (SoCal)
Posts: 7,341
Edneeis is a jewel in the rough (300+)Edneeis is a jewel in the rough (300+)Edneeis is a jewel in the rough (300+)Edneeis is a jewel in the rough (300+)
The @ is because that item is an attribute of a node. In your schema ID is an attribute of the Title node, web and topic are attributes of the Reference node.

Here is an example to show you the rest of what to do for that schema specifically.
Edneeis is offline   Reply With Quote
Old Sep 30th, 2004, 11:35 PM   #14
toytoy
Addicted Member
 
toytoy's Avatar
 
Join Date: Jul 04
Posts: 230
toytoy is an unknown quantity at this point (<10)
thanks....Edneeis

you are really so good..It solve all my problems...

Just to ask in the Enter the ID part...

the user must key in an Integer ID first before retrieve certain data..

Can it be like the user key in a string and that string is relate to the ID.. and then from there it will show all the Elements and attributes from that ID
toytoy is offline   Reply With Quote
Old Oct 1st, 2004, 12:25 AM   #15
Edneeis
Your Ad Here!
 
Edneeis's Avatar
 
Join Date: Feb 00
Location: Moreno Valley, CA (SoCal)
Posts: 7,341
Edneeis is a jewel in the rough (300+)Edneeis is a jewel in the rough (300+)Edneeis is a jewel in the rough (300+)Edneeis is a jewel in the rough (300+)
What do you mean? You want the user to search by another node or attribute? If so which one?

You can just the initial xpath to something like this to search for other things:
VB Code:
  1. 'by author
  2. Dim xpath As String = String.Format("//Book/Title[Author='{0}']", id)
  3. 'by reference topic
  4. Dim xpath As String = String.Format("//Book/Title[Reference/@topic='{0}']", id)

The search is case-sensitive by the way.
Edneeis is offline   Reply With Quote
Old Oct 1st, 2004, 12:34 AM   #16
toytoy
Addicted Member
 
toytoy's Avatar
 
Join Date: Jul 04
Posts: 230
toytoy is an unknown quantity at this point (<10)
What i means is that for example:

when i key in "France" in the textbox...

That France will point to the <Title ID ="1"> and show all the Element and attribute inside that ID to the textboxes...

and when i key in "Singapore" , It will show all the Elements and attribute from <Title ID = "2">

there is nothing to do with the content of xml files....

Just used any words to point to that particular main ID inside xml file..

Is it possible?..

Last edited by toytoy; Oct 1st, 2004 at 12:54 AM.
toytoy is offline   Reply With Quote
Old Oct 1st, 2004, 12:52 AM   #17
toytoy
Addicted Member
 
toytoy's Avatar
 
Join Date: Jul 04
Posts: 230
toytoy is an unknown quantity at this point (<10)
Another question related to the above question..

Say the Xml tag is:
Code:
<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>
Say i entered Hello in the textbox....
how can i retrieve the ID = "1" and
display the "In thing" in the textbox..

Thanks

Last edited by toytoy; Dec 3rd, 2004 at 08:05 AM.
toytoy is offline   Reply With Quote
Old Oct 1st, 2004, 01:35 AM   #18
toytoy
Addicted Member
 
toytoy's Avatar
 
Join Date: Jul 04
Posts: 230
toytoy is an unknown quantity at this point (<10)
ok.... i get what you mean...

the previous thread is solve...

what about the France and Singapore words which i post eariler

Last edited by toytoy; Oct 1st, 2004 at 01:39 AM.
toytoy is offline   Reply With Quote
Old Oct 1st, 2004, 02:00 AM   #19
Edneeis
Your Ad Here!
 
Edneeis's Avatar
 
Join Date: Feb 00
Location: Moreno Valley, CA (SoCal)
Posts: 7,341
Edneeis is a jewel in the rough (300+)Edneeis is a jewel in the rough (300+)Edneeis is a jewel in the rough (300+)Edneeis is a jewel in the rough (300+)
How is France and Singapore related to that data?

I don't understand. The data needs to be related or else how would you look it up?
Edneeis is offline   Reply With Quote
Old Oct 1st, 2004, 02:03 AM   #20
toytoy
Addicted Member
 
toytoy's Avatar
 
Join Date: Jul 04
Posts: 230
toytoy is an unknown quantity at this point (<10)
that means if that data is not declare in the Xml file, it cannot be create and point to any of the Elements or attribute...

is that what you mean...
toytoy is offline   Reply With Quote
Old Oct 1st, 2004, 02:16 AM   #21
Edneeis
Your Ad Here!
 
Edneeis's Avatar
 
Join Date: Feb 00
Location: Moreno Valley, CA (SoCal)
Posts: 7,341
Edneeis is a jewel in the rough (300+)Edneeis is a jewel in the rough (300+)Edneeis is a jewel in the rough (300+)Edneeis is a jewel in the rough (300+)
If its not in the XML document then it can't be reached via the Xpath query. How is it related? Where is it stored?
Edneeis is offline   Reply With Quote
Old Oct 1st, 2004, 02:23 AM   #22
toytoy
Addicted Member
 
toytoy's Avatar
 
Join Date: Jul 04
Posts: 230
toytoy is an unknown quantity at this point (<10)
ok....i fully understand what you mean..

now my concept on XPath is much more clearer...
I have finally resolve all my doubts..

Thanks Edneeis..

Last edited by toytoy; Oct 1st, 2004 at 02:32 AM.
toytoy is offline   Reply With Quote
Old Jan 6th, 2005, 08:59 AM   #23
toytoy
Addicted Member
 
toytoy's Avatar
 
Join Date: Jul 04
Posts: 230
toytoy is an unknown quantity at this point (<10)
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

Last edited by toytoy; Jan 6th, 2005 at 09:14 AM.
toytoy is offline   Reply With Quote
Old Jan 7th, 2005, 01:44 AM   #24
Edneeis
Your Ad Here!
 
Edneeis's Avatar
 
Join Date: Feb 00
Location: Moreno Valley, CA (SoCal)
Posts: 7,341
Edneeis is a jewel in the rough (300+)Edneeis is a jewel in the rough (300+)Edneeis is a jewel in the rough (300+)Edneeis is a jewel in the rough (300+)
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.
Edneeis is offline   Reply With Quote
Old Jan 7th, 2005, 10:15 AM   #25
toytoy
Addicted Member
 
toytoy's Avatar
 
Join Date: Jul 04
Posts: 230
toytoy is an unknown quantity at this point (<10)
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..
toytoy is offline   Reply With Quote
Old Jan 7th, 2005, 11:23 PM   #26
Edneeis
Your Ad Here!
 
Edneeis's Avatar
 
Join Date: Feb 00
Location: Moreno Valley, CA (SoCal)
Posts: 7,341
Edneeis is a jewel in the rough (300+)Edneeis is a jewel in the rough (300+)Edneeis is a jewel in the rough (300+)Edneeis is a jewel in the rough (300+)
Re: VB.NET: Get the Attribute and Text out from Xml....[Unresolved]

Then instead of calling SelectSingleNode use SelectNodes and loop through the results.
Edneeis is offline   Reply With Quote
Old Jan 11th, 2005, 12:30 AM   #27
toytoy
Addicted Member
 
toytoy's Avatar
 
Join Date: Jul 04
Posts: 230
toytoy is an unknown quantity at this point (<10)
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

Last edited by toytoy; Jan 11th, 2005 at 12:38 AM.
toytoy is offline   Reply With Quote
Old Jan 12th, 2005, 06:01 AM   #28
toytoy
Addicted Member
 
toytoy's Avatar
 
Join Date: Jul 04
Posts: 230
toytoy is an unknown quantity at this point (<10)
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
toytoy is offline   Reply With Quote
Old Jan 13th, 2005, 01:00 AM   #29
Edneeis
Your Ad Here!
 
Edneeis's Avatar
 
Join Date: Feb 00
Location: Moreno Valley, CA (SoCal)
Posts: 7,341
Edneeis is a jewel in the rough (300+)Edneeis is a jewel in the rough (300+)Edneeis is a jewel in the rough (300+)Edneeis is a jewel in the rough (300+)
Re: VB.NET: Get the Attribute and Text out from Xml....[Unresolved]

Post your code and I'll help you fix it.
Edneeis is offline   Reply With Quote
Old Jan 13th, 2005, 09:14 AM   #30
toytoy
Addicted Member
 
toytoy's Avatar
 
Join Date: Jul 04
Posts: 230
toytoy is an unknown quantity at this point (<10)
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

Last edited by toytoy; Jan 23rd, 2005 at 08:03 PM.
toytoy is offline   Reply With Quote
Old Jan 20th, 2005, 10:45 PM   #31
toytoy
Addicted Member
 
toytoy's Avatar
 
Join Date: Jul 04
Posts: 230
toytoy is an unknown quantity at this point (<10)
Re: VB.NET: Get the Attribute and Text out from Xml....[Unresolved]

Does anyone know how to solve....

Thanks
toytoy is offline   Reply With Quote
Old Jan 22nd, 2005, 12:04 AM   #32
toytoy
Addicted Member
 
toytoy's Avatar
 
Join Date: Jul 04
Posts: 230
toytoy is an unknown quantity at this point (<10)
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

Last edited by toytoy; Jan 23rd, 2005 at 08:04 PM.
toytoy is offline   Reply With Quote
Reply

Go Back   VBForums > Visual Basic > Visual Basic .NET


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 08:26 AM.




To view more projects, click here

Acceptable Use Policy


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers

Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.