Example needed: simple XML file and parsing

Discussion in 'ASP .Net' started by VB Programmer, Jan 6, 2004.

  1. 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!
    VB Programmer, Jan 6, 2004
    #1
    1. Advertising

  2. VB Programmer

    Dan Cooper Guest

    How about something like this:

    Dim dstDataSet As New DataSet
    dstDataSet.ReadXml(Server.MapPath(".") & "\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>Vegetables</name>
    </item>
    </items>


    "VB Programmer" <> wrote in message
    news:...
    > 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, Jan 6, 2004
    #2
    1. Advertising

  3. Thanks Dan! I appreciate your example.

    "Dan Cooper" <> wrote in message
    news:...
    > How about something like this:
    >
    > Dim dstDataSet As New DataSet
    > dstDataSet.ReadXml(Server.MapPath(".") & "\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>Vegetables</name>
    > </item>
    > </items>
    >
    >
    > "VB Programmer" <> wrote in message
    > news:...
    > > 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!
    > >
    > >

    >
    >
    VB Programmer, Jan 6, 2004
    #3
  4. VB Programmer wrote:

    > 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!
    >
    >


    Check out the System.Xml.XmlDocument 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.SelectNodes I believe). Here's a
    quick example snippet (sorry only example I could find quickly):

    http://www.net-language.com/CodeExample.aspx?i=185

    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.DataItem 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
    Craig Deelsnyder, Jan 6, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bob Sparks
    Replies:
    4
    Views:
    2,707
    GIMME
    Dec 16, 2003
  2. Replies:
    6
    Views:
    643
    Fuzzyman
    Jul 11, 2005
  3. Replies:
    0
    Views:
    369
  4. David Staschover

    XML::Simple Example perl help needed

    David Staschover, May 17, 2004, in forum: Perl Misc
    Replies:
    6
    Views:
    160
    Jim Cochrane
    May 18, 2004
  5. Erik Wasser
    Replies:
    5
    Views:
    450
    Peter J. Holzer
    Mar 5, 2006
Loading...

Share This Page