Populating dropdown from the XML in C#

后端 未结 5 2118
花落未央
花落未央 2021-01-06 06:00

I have got below xml format with me and I am using .NET 2.0.



  

        
相关标签:
5条回答
  • 2021-01-06 06:08

    You could use Linq-to-XML to get a collection of the values you want from the xml tree and bind that to your dropdownlist. see this link: http://msdn.microsoft.com/en-us/library/bb387061.aspx

    0 讨论(0)
  • 2021-01-06 06:11

    You could try following code, I would rather go for Xpath because it is more easier to read. DocumentLoader class, it loads content from string or any stream that has a valid xml content.

    public class DocumentLoader
        {
            private readonly string content;
            public  DocumentLoader(Stream stream):this(new StreamReader(stream).ReadToEnd())
            {
    
            }
            public DocumentLoader(string content)
            {
                this.content = content;
    
            }
            public KeyValuePair<string,string>[] GetNames()
        {
            List<KeyValuePair<string,string>> names=new List<KeyValuePair<string,string>>();
            XmlDocument document=new XmlDocument();
            document.LoadXml(this.content);
            XmlNodeList xmlNodeList = document.SelectNodes("//publication");
            if(xmlNodeList!=null)
            {
                foreach (XmlNode node in xmlNodeList)
                {
                    string key = node.InnerText;
                   string value = "";
                    if (node.Attributes != null)
                    {
                        value = node.Attributes["tcmid"].Value;
                    }
                    names.Add(new KeyValuePair<string, string>(key,value));
                }
            }
            return names.ToArray();
        }
        }
    

    Just a test code.

    public class DocumentLoaderTest
    {
        public void Test()
        {
            DocumentLoader loader = new DocumentLoader(File.Open("D:\\sampleSource.xml", FileMode.Open));
            //now names contains the value of the name element 
            List<KeyValuePair<string,string>>names=loader.GetNames();
        }
    }
    

    Updated to get Name element and tcmid attribute

    0 讨论(0)
  • 2021-01-06 06:12

    Maybe this can work for you:

    path = Path.Combine(HostingEnvironment.ApplicationPhysicalPath, @"App_Data\Empresas.xml");
    ds = new DataSet();
    ds.ReadXml(path);
    ddlEmpresa.DataValueField = "value";
    ddlEmpresa.DataTextField = "name";
    ddlEmpresa.DataSource = ds;
    ddlEmpresa.DataBind();
    

    and the XML is:

    <?xml version="1.0" encoding="utf-8" ?>
     <items>
        <item>
          <name>Elemento 1</name>
          <value>1</value>
        </item>
      </items>
    
    0 讨论(0)
  • 2021-01-06 06:26

    I have created my custom class for binding XML to DROPDOWN. code of custom class is following

    public class XmlReaderBinder
        {
            public static void BindDropDown(ComboBox dd, string elementName, string nodeNameValue,
                              string nodeNameText, string xmlPath)
            {
                HttpContext context = HttpContext.Current;
                XmlTextReader reader = null;
                ArrayList lstItems = new ArrayList();
                string val = String.Empty;
                string txt = String.Empty;
                bool inTarget = false;
                try
                {
                    reader = new XmlTextReader(xmlPath);
                    //Allow for object to object comparison rather than string comparison
                    object target = reader.NameTable.Add(elementName);
                    object targetVal = reader.NameTable.Add(nodeNameValue);
                    object targetTxt = reader.NameTable.Add(nodeNameText);
                    //Read through the XML stream and find proper tokens
                    while (reader.Read())
                    {
                        if (reader.NodeType == XmlNodeType.Element)
                        {
                            //Check attribute names
                            if (reader.Name.Equals(target))
                            {
                                inTarget = true;
                                //Get attribute values (if any)
                                if (reader.HasAttributes)
                                {
                                    if (reader.MoveToAttribute(nodeNameValue))
                                    {
                                        val = reader.Value;
                                        reader.MoveToElement();
                                    }
                                    if (reader.MoveToAttribute(nodeNameText))
                                    {
                                        txt = reader.Value;
                                        reader.MoveToElement();
                                    }
                                }
                                if (val == "" || txt == "") val = txt = String.Empty;
                                if (val != String.Empty && txt != String.Empty)
                                {
                                    ListItem item = new ListItem(txt, val);
                                    lstItems.Add(item);
                                    //Attribute values override any child nodes values
                                    //so if we match on attributes then don't look at any 
                                    //child nodes
                                    inTarget = false;
                                    val = txt = String.Empty;
                                }
                            }
                            else if (inTarget)
                            {  //Check for child nodes that match
                                if (reader.Name.Equals(targetVal))
                                {
                                    val = reader.ReadString();
                                    if (val == "") val = String.Empty;
                                }
                                if (reader.Name.Equals(targetTxt))
                                {
                                    txt = reader.ReadString();
                                    if (txt == "") txt = String.Empty;
                                }
                                if (val != String.Empty && txt != String.Empty)
                                {
                                    ListItem item = new ListItem(txt, val);
                                    lstItems.Add(item);
                                    val = txt = String.Empty;
                                }
                            }
                        }
                        if (reader.NodeType == XmlNodeType.EndElement || reader.IsEmptyElement)
                        {
                            if (reader.Name.Equals(target))
                            {
                                inTarget = false;
                            }
                        }
    
                    }
                    if (lstItems.Count > 0)
                    {
                        foreach (ListItem item in lstItems)
                        {
                            dd.Items.Add(item);
                        }
    
                    }
                    else
                    {
                        ListItem item = new ListItem("No Data Available", "");
                        dd.Items.Add(item);
                    }
                    lstItems.Clear();
                }
                catch (Exception exp)
                {
                    context.Response.Write(exp.Message);
                }
                finally
                {
                    if (reader != null) reader.Close();
                }
            }
    
        }
    

    And after creating class you can call class function 'BindDropDown' like below mentioned ways.

    XmlReaderBinder.BindDropDown('DROP DOWN NAME', "publicationsList", "publication", "Name", xmlPath);
    

    Hope it will help for you.

    0 讨论(0)
  • 2021-01-06 06:27

    You could do something like this

    Using Linq

    XDocument xDoc = XDocument.Load(@"Yourxmlfile.xml");
                var query = from xEle in xDoc.Descendants("publication")
                            select new ListItem(xEle.Element("name").Value, xEle.Attribute("tcmid").Value);
    
                ddlList.DataValueField = "value";
                ddlList.DataTextField = "text";
                ddlList.DataSource = query;
                ddlList.DataBind();
    

    Update: Using XmlDocument

    XmlDocument xDocument = new XmlDocument();
                xDocument.Load(@"YourXmlFile.xmll");
                foreach (XmlNode node in xDocument.GetElementsByTagName("publication"))
                {
                    ddlList.Items.Add(new ListItem(node.SelectSingleNode("name").InnerText,
                        node.Attributes["tcmid"].Value));
                }
                ddlList.DataValueField = "value";
                ddlList.DataTextField = "text";            
                ddlList.DataBind();
    
    0 讨论(0)
提交回复
热议问题