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"/>
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?
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)
{
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
Please remember to click "Mark as Answer" the responses that resolved your issue :) ~
Member
42 Points
241 Posts
Accessing XML Data with Bound DropDown List
Jan 10, 2020 05:06 PM|TRIMS30|LINK
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 Sub
Member
96 Points
54 Posts
Re: Accessing XML Data with Bound DropDown List
Jan 10, 2020 06:40 PM|Rebin.O.Q|LINK
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
##ASPX page
##The Code behind
I hope it helps you.
Please remember to click "Mark as Answer" the responses that resolved your issue :) ~
Member
42 Points
241 Posts
Re: Accessing XML Data with Bound DropDown List
Jan 10, 2020 10:35 PM|TRIMS30|LINK
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
Member
96 Points
54 Posts
Re: Accessing XML Data with Bound DropDown List
Jan 11, 2020 10:02 AM|Rebin.O.Q|LINK
Hi , you can use online converter C# to VB.NET http://converter.telerik.com/
## VB.Net Code
Please remember to click "Mark as Answer" the responses that resolved your issue :) ~