Hope you are doing well and happy with your life whatever you are doing. I have a big question. Well, it's a big job for me. I tried to google and follow some tutorials frist but finally realise I need help from stackoverflowers.
I have a one big xml file such below. I have no control over its structure as it already exists with data.
<?xml version="1.0" encoding="utf-8" ?>
<root>
<Category Cat="A">
<SubLevel Name="Sub1">
<option name="a" />
<option name="b" />
</SubLevel>
<SubLevel Name="Sub2">
<option name="a" />
<option name="b" />
</SubLevel>
</Category>
<Category Cat="B">
<SubLevel Name="Sub1">
<option name="a" />
<option name="b" />
</SubLevel>
<SubLevel Name="Sub2">
<option name="a" />
<option name="b" />
</SubLevel>
</Category>
</root>
I have a aspx page with a dropdownlist (for Cat eg."A"), radiobuttonlist(for Sublevel eg. "Sub1") and checkboxlist(for option eg."a").
The idea is if a user select Category A from dropdownlist, the Radiobuttonlist will be filled with corresponding data (eg. Sub1, Sub2). if the user select Sub1 from radiobuttonlist, the checkboxlist will be filled with option data (eg. "a, b")
I have the interface but I don't have much code yet. I can either load the xml into XMLDocument or a Dataset but don't know how to continue Filling/Filtering and Querying data. Need the code in vb.net. No preference over XMLDocument/Dataset. I can use whatever. So How can I achieve this?
THanks so much for your help. (PS. i didn't write down the partial codes I have coz I want it to be flexible for u)
I'd use LINQ to XML. Let's assume that your XML is hard coded and you only have one combobox on the form you are using. So, this code:
Imports Systeml.Linq, System.Xml.Linq
Public Class Form1
Dim myxml As XDocument
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
myxml = <?xml version="1.0" encoding="utf-8"?>
<root>
<Category Cat="A">
<SubLevel Name="Sub1">
<option name="a"/>
<option name="b"/>
</SubLevel>
<SubLevel Name="Sub2">
<option name="a"/>
<option name="b"/>
</SubLevel>
</Category>
<Category Cat="B">
<SubLevel Name="Sub1">
<option name="a"/>
<option name="b"/>
</SubLevel>
<SubLevel Name="Sub2">
<option name="a"/>
<option name="b"/>
</SubLevel>
</Category>
</root>
Dim query = From node In myxml...<Category>
Order By node.@Cat
Select New With {.Category = node.@Cat.ToString()}
With ComboBox1
.DataSource = query.ToArray()
.DisplayMember = "Category"
.ValueMember = "Category"
End With
End Sub
will populate the combobox with the first level. Now, in the combobox's Click event, you need a query like
Dim q2= From node2 In myxml...<SubLevel>
Where node.Parent.@Cat = ComboBox1.Text
Order By node.@Name
Select New With {.Name = node.@Name.ToString()}
from which you can then iterate the sublevel Names using a For Each statement, and load up your radiobutton control dynamically (tagging the controls with the name of the sublevel). And so on for the checkboxes. You can load up the myxml variable by using XDocument.Load()
Hope this helps. VB's XML handling is one for the few areas in which it socres decisively over C#, so if you have a lot of XML to handle, I'd stick to VB.NET