Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with XML documents and loops?
#1
I am not a master at the VB language. I am fairly new to actually trying to learn the language. I am writing a program, that requires that needs to do the following things:

* Read the XML document
* Create a ToolStripButton for each childnode in the XMLDocument with the text <name>
* Add handlers tI each button created that, when clicked, will navigate a web browser to the text in one of the other childnodes <URL>
* Change other properties according to diffrent buttons to diffrent child nodes.

I might have child nodes wrong, I'm not a master at XML documents.

But if you need to see the code I previously tried, I'll have to get on my computer when I get home.

I know how to do it in c#, but how would I do it in vb?
#2
To read the XML file you are going to want to use an XMLTextReader object.

Specifically, let's break down your task here.
1) Read the XML document.
The first thing you want to do is create a XML Document variable, so, let's say that your XML Document is stored in "doc.xml".

First, let's create the variable:
[code2=vbnet]Dim xml_doc As New Xml.XmlDocument[/code2]

Now we need to assign the xml_doc variable to the XML document on the disk:
[code2=vbnet]xml_doc.Load("doc.xml")[/code2]

Now that the XML variable is setup, let's go about your next problems.
* Create a ToolStripButton for each childnode in the XMLDocument with the text <name>
Well, this is relatively simple with our current setup. So let's scroll through all the childnodes and create a ToolStripButton for them all!
[code2=vbnet]For Each node As Xml.XmlNode In xml_doc.ChildNodes
Dim button As New ToolStripButton
button.Text = node.Attributes("name").InnerText
button.Tag = node.Attributes("URL").InnerText
AddHandler button.Click, AddressOf TSButtonClick
Next[/code2]

In this snippet of code we also store the URL that the button links to inside the buttons "tag" property, and add a handler that links it to another method when the button is clicked. So, we just need to create the body of that method:
[code2=vbnet]Sub TSButtonClick(sender As Object, e As System.EventArgs)
Dim button As ToolStripButton = CType(sender, ToolStripButton)
Process.Start(button.Tag)
End Sub[/code2]

And voila! We have a working program (I hope). I have not tested it, so please correct me if it doesn't work.
My Blog | My Setup | My Videos | Have a wonderful day.
#3
It comes up with a message box that says this:
"An unhandled exception of type 'System.NullReferenceException' occurred in Eclipse.exe

Additional information: Object reference not set to an instance of an object."

And then it highlights:
Code:
button.Tag = node.Attributes("URL").InnerText

Any idea? <!-- s:? --><img src="{SMILIES_PATH}/icon_e_confused.gif" alt=":?" title="Confused" /><!-- s:? -->
#4
What you refer to as child nodes are nothing more than a simple path just as in windows explorer if used right.
Now Brandonio forgot to mention the correct format of xml file to use.
In the folowing example i will show you what i consider to be the easiest Xml format and code snippet to use:

1. Xml Format:
Code:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<root>
    <ToolStipButtons>
        <item Name="btn1" url="www.google.com" />
        <item name="btn2" url="www.bing.com" />
    </ToolStripButtons>
    
</root>
2. Create the variable:
Just as Brandonio said create a new xml_doc variable and load your document
[code2=vbnet]Dim Xml_Doc As Xml.XmnDocument
Xml_Doc.load("path to xml file")[/code2]

3. Loop through nodes to create buttons:

[code2=vbnet]Dim node As Xml.XmlNode
For Each node In xml_doc.selectnodes("/root/ToolStrpButtons/item")'the path of the xml child node
Dim btn As New ToolStripButton _
With {.Name = node.Attributes("name").Value.ToString, _
.Tag = node.Attributes("url").Value.ToString}
AddHandler Button.Click, AddressOf TSButtonClick
Next[/code2]

Now add the same code for the handler as shown in brandonio's example and all should be fine.

If you have any errors or don't understand the coding above leave a reply and i'll be happy to help.
Sssssssssoftware developer...
#5
Thank you all for your answers, and it is now running
Yet, the button will not show
I've tried:
Code:
toolStrip1.Items.Add(btn)

But, it'll just show a blank toolstrip.

Is there anything I'm doing wrong? Sorry for probably wasting your time with such an amateurish question, I should know this, but apparently I don't.
#6
Well that should be a simple problem to solve. If you used the code above without changing anything then the new buttons have no text or image and that's why you cannot see them but they are there just hover with the mouse over the toolstrip and you will see

To solve this just add another atribute to the xml file like this
Code:
<item name="btn" url="some Url" text="buton text">

Then set the value for the buton's text property inside the for loop
[code2=vbnet]For Each node In xml_doc.selectnodes("/root/ToolStrpButtons/item") 'the path of the xml child node
Dim btn As New ToolStripButton _
With {.Name = node.Attributes("name").Value.ToString, _
.Tag = node.Attributes("url").Value.ToString, _
.text = node.Attributes("text").Value.ToString}
AddHandler Button.Click, AddressOf TSButtonClick
Next[/code2]

Now the butons should be visible
Sssssssssoftware developer...
#7
Thanks a lot, Snake_eyes, for your input! I have never worked with XML files in VB.NET before, so you saved the day. Thanks for explaining the structure of XML and its correlation to VB.NET!
My Blog | My Setup | My Videos | Have a wonderful day.
#8
Yes, thank you a lot. It really helped a lot <!-- sBig Grin --><img src="{SMILIES_PATH}/icon_e_biggrin.gif" alt="Big Grin" title="Very Happy" /><!-- sBig Grin -->
#9
Glad i could help you .

If you need the code to write the xml file just say so. I will open a new topic in the "Code Snippets" section explaining how to read and write an xml file
Here is the link to that thread http://bpforums.info/viewtopic.php?f=22&...fd6e5e2051

BTW Brandonio i have a question Why does the code i posted show in green instead? is there something i'm missing?
Sssssssssoftware developer...
#10
Snake_eyes Wrote:BTW Brandonio i have a question Why does the code i posted show in green instead? is there something i'm missing?
We actually have two code handling tags on this forum. You are using the default tag. If you would like to use the codebox that appears in most of my posts, you need to use the code2 tag instead of the code tag. Full explanation can be seen here: <!-- l --><a class="postlink-local" href="http://bpforums.info/viewtopic.php?f=12&t=569">viewtopic.php?f=12&t=569</a><!-- l -->
My Blog | My Setup | My Videos | Have a wonderful day.


Forum Jump:


Users browsing this thread: 1 Guest(s)