Avatar of vcharles
vcharles
 asked on

Help with loading data from one xml file to multiple Listboxes

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
ASP.NET.NET ProgrammingVisual Basic.NET

Avatar of undefined
Last Comment
vcharles

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Rose Babu

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
vcharles

ASKER
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>
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy