Help with loading data from one xml file to multiple Listboxes

vcharles
vcharles used Ask the Experts™
on
Hi,

If I have an xml file with the following data. I am tryin to minimize the number of xml files in my project.

<Root>
<CountryTable>
<Country_ID>1</Country_ID>
<Country>BEL</Country>
</CountryTable>
<CountryTable>
<Country_ID>2</Country_ID>
<Country>CAN</Country>
</CountryTable>
<ItemTable>
<Item_ID>1</Item_ID>
<Item>ItemA</Item>
</ItemTable>
<ItemTable>
<Item_ID>2</Item_ID>
<Item>ItemB</Item>
</ItemTable>
</Root>

How do I load Listbox1 with CountryTable data and Listbox2 with ItemTable data?

Currently I ma using the following approach to load the components when they are in separate xml files.


        Dim CountryUser As New DataSet
        CountryUser.ReadXml(Server.MapPath("~/App_Data/CountryUser.xml"))
        C1ACountryUser.DataSource = CountryUser.Tables(0)
        C1ACountryUser.DataMember = "Country"
        C1ACountryUser.DataTextField = "Country"
        C1ACountryUser.DataValueField = "Country_ID"
        C1ACountryUser.DataBind()

Victor
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Senior Team Manager
Commented:
Hi, Try like this

XMLFile.xml

<?xml version="1.0" encoding="utf-8" ?>
<Root>
  <CountryTable>
    <Country_ID>1</Country_ID>
    <Country>BEL</Country>
  </CountryTable>
  <CountryTable>
    <Country_ID>2</Country_ID>
    <Country>CAN</Country>
  </CountryTable>
  <ItemTable>
    <Item_ID>1</Item_ID>
    <Item>ItemA</Item>
  </ItemTable>
  <ItemTable>
    <Item_ID>2</Item_ID>
    <Item>ItemB</Item>
  </ItemTable>
</Root>

Open in new window

Code Part:

Dim ds As New DataSet
ds.ReadXml(Server.MapPath("XMLFile.xml"))

Dim dt1 As New DataTable
dt1 = ds.Tables(0)

Dim dt2 As New DataTable
dt2 = ds.Tables(1)

ListBox1.DataSource = dt1
ListBox1.DataTextField = "Country"
ListBox1.DataValueField = "Country_ID"
ListBox1.DataBind()

ListBox2.DataSource = dt2
ListBox2.DataTextField = "Item"
ListBox2.DataValueField = "Item_ID"
ListBox2.DataBind()

Open in new window

Hope this will help you...

Author

Commented:
Thanks, How would i modify the code below to accomodate using the approach above?
For example Item, ItemA and Country are combined in XMLFile.xml as shown in Part B below.

Dim xyz() As String = New String() {"Item", "ItemA", "Country"}

        For i = 0 To 2

            Dim curXYZ = xyz(i)
            Dim MyFix As XDocument = XDocument.Load(Server.MapPath("~/App_Data/" & curXYZ & ".xml"))

            Dim xid As String

            xid = curXYZ & "_ID"

            Dim LargestFixID = (From el In MyFix.Descendants(curXYZ & "Table") Select CInt(el.<xid>.Value)).ToList
            LargestFixID.Sort()

            Dim xtable As String

            xtable = curXYZ & "Table"
            Dim ctrl As Control = MultiView1.FindControl("C1" & curXYZ)
            Dim Txt As String = Nothing
            If TypeOf (ctrl) Is C1ComboBox Then
                Txt = CType(ctrl, C1ComboBox).Text
            End If

            Dim CheckForItemFix = (From el In MyFix.Descendants(xtable).Elements(curXYZ) Select el.Value).ToList


            'Check if value is already in xml files, if not add it to the xml files
            If Not CheckForItemFix.Contains(Txt) Then

                Dim xNew As XElement = New XElement(xtable)

                xNew.Add(New XElement(xid, (LargestFixID.Count() + 1)))

                xNew.Add(New XElement(curXYZ, Txt))

                MyFix.Root.Add(xNew)

                MyFix.Save(Server.MapPath("~/App_Data/" & curXYZ & ".xml"))

                'Prepare variable to add to Link.xml file

                Select Case curXYZ
                    Case "Item"
                        searchIdItem = LargestFixID.Count() + 1
                    Case "ItemA"
                        searchIdItemA = LargestFixID.Count() + 1
                    Case "Country"
                   searchIdCountry = LargestFixID.Count() + 1
                  End Select
            End If


Part B:

<?xml version="1.0" encoding="utf-8" ?>
<Root>
  <CountryTable>
    <Country_ID>1</Country_ID>
    <Country>BEL</Country>
  </CountryTable>
  <CountryTable>
    <Country_ID>2</Country_ID>
    <Country>CAN</Country>
  </CountryTable>
  <ItemTable>
    <Item_ID>1</Item_ID>
    <Item>ItemA</Item>
  </ItemTable>
  <ItemTable>
    <Item_ID>2</Item_ID>
    <Item>ItemB</Item>
  </ItemTable>
 <ItemTableA>
    <ItemA_ID>1</ItemA_ID>
    <ItemA>ItemA</ItemA>
  </ItemTableA>
  <ItemTableA>
    <ItemA_ID>2</Item_ID>
    <ItemA>ItemB</ItemA>
  </ItemTableA>
</Root>

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial