Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Filling a Drop down from an XML
Message
De
13/01/2011 10:28:05
 
 
À
13/01/2011 09:02:51
Timothy Bryan
Sharpline Consultants
Conroe, Texas, États-Unis
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Divers
Thread ID:
01496032
Message ID:
01496040
Vues:
41
>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...
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform