Example needed: simple XML file and parsing

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • VB Programmer

    Example needed: simple XML file and parsing

    A dropdownlist in my ASP.NET webform needs to be populated from values taken
    from an XML file. Can someone provide a (simple) sample XML file and a
    parsing routine using VB.NET?

    The values could be something like:
    Fruits
    Vegetables
    Meats
    Dairy

    Thanks in advance!


  • Dan Cooper

    #2
    Re: Example needed: simple XML file and parsing

    How about something like this:

    Dim dstDataSet As New DataSet
    dstDataSet.Read Xml(Server.MapP ath(".") & "\Items.xml ")
    With DropDownList1
    .DataSource = dstDataSet
    .DataValueField = "id"
    .DataTextField = "name"
    .DataBind()
    End With

    Then create an Items.xml file that contains:

    <?xml version="1.0" encoding="utf-8" ?>
    <items>
    <item>
    <id>1</id>
    <name>Fruits</name>
    </item>
    <item>
    <id>2</id>
    <name>Vegetable s</name>
    </item>
    </items>


    "VB Programmer" <growNO-SPAM@go-intech.com> wrote in message
    news:ey9OoaF1DH A.2948@TK2MSFTN GP09.phx.gbl...[color=blue]
    > A dropdownlist in my ASP.NET webform needs to be populated from values[/color]
    taken[color=blue]
    > from an XML file. Can someone provide a (simple) sample XML file and a
    > parsing routine using VB.NET?
    >
    > The values could be something like:
    > Fruits
    > Vegetables
    > Meats
    > Dairy
    >
    > Thanks in advance!
    >
    >[/color]


    Comment

    • VB Programmer

      #3
      Re: Example needed: simple XML file and parsing

      Thanks Dan! I appreciate your example.

      "Dan Cooper" <dan.cooper@tis cali.co.uk> wrote in message
      news:OYKB20F1DH A.540@tk2msftng p13.phx.gbl...[color=blue]
      > How about something like this:
      >
      > Dim dstDataSet As New DataSet
      > dstDataSet.Read Xml(Server.MapP ath(".") & "\Items.xml ")
      > With DropDownList1
      > .DataSource = dstDataSet
      > .DataValueField = "id"
      > .DataTextField = "name"
      > .DataBind()
      > End With
      >
      > Then create an Items.xml file that contains:
      >
      > <?xml version="1.0" encoding="utf-8" ?>
      > <items>
      > <item>
      > <id>1</id>
      > <name>Fruits</name>
      > </item>
      > <item>
      > <id>2</id>
      > <name>Vegetable s</name>
      > </item>
      > </items>
      >
      >
      > "VB Programmer" <growNO-SPAM@go-intech.com> wrote in message
      > news:ey9OoaF1DH A.2948@TK2MSFTN GP09.phx.gbl...[color=green]
      > > A dropdownlist in my ASP.NET webform needs to be populated from values[/color]
      > taken[color=green]
      > > from an XML file. Can someone provide a (simple) sample XML file and a
      > > parsing routine using VB.NET?
      > >
      > > The values could be something like:
      > > Fruits
      > > Vegetables
      > > Meats
      > > Dairy
      > >
      > > Thanks in advance!
      > >
      > >[/color]
      >
      >[/color]


      Comment

      • Craig Deelsnyder

        #4
        Re: Example needed: simple XML file and parsing

        VB Programmer wrote:
        [color=blue]
        > A dropdownlist in my ASP.NET webform needs to be populated from values taken
        > from an XML file. Can someone provide a (simple) sample XML file and a
        > parsing routine using VB.NET?
        >
        > The values could be something like:
        > Fruits
        > Vegetables
        > Meats
        > Dairy
        >
        > Thanks in advance!
        >
        >[/color]

        Check out the System.Xml.XmlD ocument class, it has load methods, etc. to
        load in the doc or string. Then you use XPath to query and select the
        nodes with your values (XmlDocument.Se lectNodes I believe). Here's a
        quick example snippet (sorry only example I could find quickly):



        If it's decent XML, all of your values should be selectable at once (one
        method call like above). Then you can bind the resulting XmlNodeList to
        your dropdown. Each Container.DataI tem will then be an XmlNode.

        In the dropdown, set the DataTextField to the name of whatever property
        of the XmlNode is the one to display on the screen (ie. "Text") and the
        DataValueField as well (probably the same unless you have some other
        attribute, etc. that is used for the value).


        --
        Craig Deelsnyder
        Microsoft MVP - ASP.NET

        Comment

        Working...