Asked by:
Accessing XML Data with Bound DropDown List

Question
-
User-72198832 posted
Am trying to use DropDown list bound to an XML data control to select some data from XML file.
I can easily get DataField and ValueField data - How do i access other fields in selected item?Here's some related code below.
Any help is appreciated
Lee
Xml Data
<Inventory Stock_No="D-362" Description="OIL FILTER" On_Hand="12" Units="EA" Count="0" Change="0" SortKey="6845515450"/>Markup
<asp:DropDownList ID="lstInventory" runat="server" Font-Bold="True" Font-Names="Arial" DataSourceID="XmlInvList" DataTextField="Stock_No" DataValueField="Description" AutoPostBack="True">
</asp:DropDownList>Code Behind
Private Sub lstInventory_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lstInventory.SelectedIndexChanged
txtItemDescr.Text = lstInventory.SelectedValue
'Im able to get both Stock Number and Description fields here. How do I get ON_HAND and UNITS values from selected item to display in text boxes?'txtOnHand.text=????
'txtUnits.text=????
End SubFriday, January 10, 2020 5:06 PM
All replies
-
User-1519014291 posted
Hi TRIMS30, just use linq to xml
the following code worked for me well I got both On_Hand and Units value##the xml file
<?xml version="1.0" encoding="utf-8" ?> <InventoryRoot> <Inventory Stock_No="D-362" Description="OIL FILTER" On_Hand="12" Units="EA" Count="0" Change="0" SortKey="6845515450"/> </InventoryRoot>
##ASPX page
<asp:DropDownList ID="lstInventory" AppendDataBoundItems="true" runat="server" Font-Bold="True" Font-Names="Arial" DataSourceID="XmlInvList" DataTextField="Stock_No" DataValueField="Description" AutoPostBack="True" OnSelectedIndexChanged="lstInventory_SelectedIndexChanged"> <asp:ListItem Value="-1">Select</asp:ListItem> </asp:DropDownList> <asp:XmlDataSource ID="XmlInvList" runat="server" DataFile="~/XMLFile1.xml"></asp:XmlDataSource>
##The Code behind
protected void lstInventory_SelectedIndexChanged(object sender, EventArgs e) { XDocument document = XDocument.Load(Server.MapPath("~/XMLFile1.xml")); var query = from r in document.Descendants("Inventory") where (string)r.Attribute("Stock_No").Value == (lstInventory.SelectedItem.ToString()) select new { On_Hand = r.Attribute("On_Hand").Value, Units = r.Attribute("Units").Value }; foreach (var item in query) {
txtOnHand.text=item.On_Hand;
txtUnits.text=item.Units;
//Response.Write(item.On_Hand); //Response.Write(item.Units);
}I hope it helps you.
Friday, January 10, 2020 6:40 PM -
User-72198832 posted
Rebin:
Thanks for the example - Unfortunately Im working in VB.Net not C# and not to well versed on c# syntax.
If possible can you show me example in VB.Net ?
Lee
Friday, January 10, 2020 10:35 PM -
User-1519014291 posted
Hi , you can use online converter C# to VB.NET http://converter.telerik.com/
## VB.Net Code
Protected Sub lstInventory_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Dim document As XDocument = XDocument.Load(Server.MapPath("~/XMLFile1.xml")) Dim query = From r In document.Descendants("Inventory") Where CStr(r.Attribute("Stock_No").Value) = (lstInventory.SelectedItem.ToString()) Select New With {Key .On_Hand = r.Attribute("On_Hand").Value, Key .Units = r.Attribute("Units").Value } For Each item In query txtOnHand.text = item.On_Hand txtUnits.text = item.Units Next End Sub
Saturday, January 11, 2020 10:02 AM