Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Filling a Drop down from an XML
Message
From
14/01/2011 03:54:01
 
 
To
13/01/2011 17:26:01
Timothy Bryan
Sharpline Consultants
Conroe, Texas, United States
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Miscellaneous
Thread ID:
01496032
Message ID:
01496123
Views:
29
>>>Hi All,
>>>
>>>I am trying to fill some drop down list boxes in ASP.Net from xml files. I have not succeded for some reason after several different methods of doing so. The fact it is an asp application isn't really relevant to my problem but more along the lines of I apparently don't know how to navigate the xml properly.
>>>
>>>Here is a sample of my xml file - (Sorry had to change all my left and right angle brackets to parens to get them to display)
>>>
>>>(prefixes)
>>>  (prefix Title="Mr" /)
>>>  (prefix Title="Mrs" /)
>>>  (prefix Title="Miss" /)
>>>  (prefix Title="Ms" /)
>>>  (prefix Title="Prof" /)
>>>  (prefix Title="Capt" /)
>>>  (prefix Title="Dr" /)
>>>  (prefix Title="Rev" /)
>>>(/prefixes)
>>>
>>>
>>>I also have another xml for states where I need the Two letter ID as well.
>>>
>>>(states)
>>>  (state Name="Alabama" ID="AL" /)
>>>  (state Name="Alaska" ID="AK" /)
>>>  (state Name="Arizona" ID="AZ" /)
>>>  (state Name="Arkansa" ID="AR" /)
>>>  (state Name="California" ID="CA" /)
>>>
>>>
>>>I have tried seperating the Name and ID as different nodes as well
>>>
>>>(states)
>>>  (state)
>>>    (Name)Alabama(/Name)
>>>    (ID)AL(/ID)
>>>  (/state)
>>>(/states)
>>>
>>>
>>>One of my attempts to bind this to a combo box was in asp such as this
>>>
>>>(asp:XmlDataSource ID="xmlPrefixes" runat="server" DataFile="~/Xml/Prefixes.xml" XPath="prefixes/prefix" /)
>>>(mm:mmRADComboBox ID="cboNamePrefix" runat="server"
>>>                        DataSourceID="xmlPrefixes" DataTextField="Title" DataValueField="Title"
>>>                        Width="60px" /)
>>>
>>>Couldn't get this to work so I resorted to code and tried this:
>>>
>>>
>>>XmlDocument doc = new XmlDocument();
>>>doc.Load(Server.MapPath("/Xml/States.xml"));
>>>XmlNodeList states = doc.SelectNodes("//state/Name");
>>>
>>>for (int i = 0; i < states.Count; i++)
>>>{
>>>    this.cboState.Items.Add(new Telerik.Web.UI.RadComboBoxItem(states[i].InnerText, states[i].InnerText));
>>>}
>>>
>>>In code the xml document loads up just fine, but navigating the nodes is the problem
>>>
>>>
>>>Any help to get me on the right track would be appreciated. Also tried a linq method but not successful.
>>>Thanks
>>>Tim
>>
>>How about XDocument:
public class StateInfo
>>    {
>>        public string Id { get; set; }
>>        public string Name { get; set; }
>>    }
Then:
           XDocument doc = XDocument.Load("States.xml");
>>
>>            var theStates = from state in doc.Descendants("state")
>>                            select new StateInfo()
>>                                {
>>                                    Name = (string)state.Attribute("Name"),
>>                                    Id = (string)state.Attribute("ID")
>>                                };
and you should be able to bind to theStates. Could bind in markup using WPF but I don't know how you'd do that in ASP.NET...
>
>I had tried to use a linq version withou success such as this.
>
>XmlDocument doc = new XmlDocument();
>doc.Load(Server.MapPath("/Xml/States.xml"));
>
>for (int i = 0; i < 50; i++)
>{
>this.cboState.Items.Add(new Telerik.Web.UI.RadComboBoxItem(doc.SelectNodes("/states/state[i]/name").ToString(), doc.SelectNodes("/states/state[i]/Id").ToString()));
>}
>
Don't like that. What if Texas pulls out of the union. :-} How about:
foreach (XmlElement x in doc.SelectNodes("/states/state"))
            {
                someComboBox.Items.Add(x.GetAttribute("Name"));
            }
>When I use the code you show I get several errors on the syntax checker.
>red line under f on from indicates Unknown method 'Select(?)' of System.Collections.Generic.IEnumerable(System.Xml.Linq.XElement)'
>and on the state variables it indicates (range variable) TRow state is Unknown type of variable 'state'

Just checked and the code worked for me with your first version of the states.xml (using attributes)
Sounds as if maybe you don't have a reference to System.Linq in your usings....
HTH
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform