?
Solved

How do I create a module to parse a XML file and then populate differnt drop down menues?

Posted on 2009-03-30
4
Medium Priority
?
199 Views
Last Modified: 2013-11-26
Hello everybody,

I am very new to XML specially when it comes to software developing. I've work with it before but in web development. A frind of mine asked me to create a program using VB.NET to control his store and I thought that using XML was the best choice because of performance and it's easy to update.

I've created a XML file which holds the items he sells but trully, I don't know how to create a function inside a module to handle that file and then use it to populate different fields (text boxes, combo boxes, etc.) in a form.

The structure I'm using within XML is this:

<?xml version="1.0" encoding="utf-8"?>
<Items>
  <Item>
    <ID>10001</ID>
    <Name>Item 1</Name>
    <Type>Food</Type>
    <Weight>31</Weight>
    <Price>9.99</Price>
  </Item>
  <Item>
    <ID>10002</ID>
    <Name>Item 2</Name>
    <Type>Tool</Type>
    <Weight>700</Weight>
    <Price>39.99</Price>
  </Item>
</NewData>

I would like for example, to be able to display all food items in a grid or whatever, only when the Food cathegory is selected in a dropdown menu.

How can I do this?

Thanks in advance!
0
Comment
Question by:Cesar Aracena
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 2

Expert Comment

by:devlinb
ID: 24022492
You could write the xml file into a dataset then assign the datasource of the grid to this dataset:
Dim ds As New DataSet
ds.WriteXml("C:\filename.xml")
DataGridView1.DataSource = ds

Open in new window

0
 
LVL 27

Accepted Solution

by:
nmarun earned 2000 total points
ID: 24022692
Here's what I have:

my xml:
<?xml version="1.0" encoding="utf-8"?>
<Items>
  <Item>
    <ID>10001</ID>
    <Name>Item 1</Name>
    <Type>Food</Type>
    <Weight>31</Weight>
    <Price>9.99</Price>
  </Item>
  <Item>
    <ID>10002</ID>
    <Name>Item 2</Name>
    <Type>Tool</Type>
    <Weight>700</Weight>
    <Price>39.99</Price>
  </Item>
  <Item>
    <ID>10003</ID>
    <Name>Item 3</Name>
    <Type>Food</Type>
    <Weight>15</Weight>
    <Price>9.99</Price>
  </Item>
  <Item>
    <ID>10004</ID>
    <Name>Item 4</Name>
    <Type>Tool</Type>
    <Weight>55</Weight>
    <Price>250</Price>
  </Item>
</Items>
 
my aspx:
<asp:DropDownList ID="ddlTest" runat="server" AutoPostBack="true"
    onselectedindexchanged="ddlTest_SelectedIndexChanged" />
<br />
 
<asp:Repeater ID="rptTest" runat="server" >
    <ItemTemplate>
    ID: <%#((XmlNode)Container.DataItem)["ID"].InnerText%>
    <br />
    Name: <%#((XmlNode)Container.DataItem)["Name"].InnerText%>
    <br />
    Weight: <%#((XmlNode)Container.DataItem)["Weight"].InnerText%>
    <br />
    Price: <%#((XmlNode)Container.DataItem)["Price"].InnerText%>
    <br />
    </ItemTemplate>
</asp:Repeater>
 
my code-behind:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    If Not IsPostBack Then
        Dim xmlDocument As New XmlDocument()
        xmlDocument.Load(Server.MapPath("SiteMapNode.xml"))
        Dim types As XmlNodeList = xmlDocument.SelectNodes("//Items/Item/Type")
        For i As Integer = 0 To types.Count - 1
            Dim li As New ListItem(types(i).InnerText, types(i).InnerText)
            If Not ddlTest.Items.Contains(li) Then
                ddlTest.Items.Add(li)
            End If
        Next
    End If
End Sub
 
Protected Sub ddlTest_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
    Dim xmlDocument As New XmlDocument()
    xmlDocument.Load(Server.MapPath("SiteMapNode.xml"))
    Dim nodes As XmlNodeList = xmlDocument.SelectNodes("/Items/Item[Type='" & ddlTest.SelectedItem.Value & "']")
    rptTest.DataSource = nodes
    rptTest.DataBind()
    
End Sub

Open in new window

0
 
LVL 27

Expert Comment

by:nmarun
ID: 24022715
Make sure you add this line in your aspx page just below the <%@ Page.. declaration:
<%@ Import Namespace="System.Xml"%>
and
Imports System.Xml
line in the code-behind file.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question