简体   繁体   中英

Populating dropdown from the XML in C#

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

<?xml version="1.0" encoding="utf-8"?>
<publicationsList>
  <publication tcmid="tcm:0-226-1">
    <name>00 Primary Parent</name>
  </publication>
  <publication tcmid="tcm:0-227-1">
    <name>01 Group Parent</name>
  </publication>
  <publication tcmid="tcm:0-228-1">
    <name>02 Developer Library</name>
  </publication>
  <publication tcmid="tcm:0-229-1">
    <name>03C Content Library</name>
  </publication>
</publicationsList>

Now I want to populate my dropdownlist from the above XML, my dropdownlist TEXT will be "name" node value and dropdownlist VALUE will be "tcmid" attribute value using a method in C#.

Please suggest!!

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();

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>

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM