开发者

Adding XML values to dropdownlist, c#

I've been messing around with the following code to add each node in an xml fileto a dropdown list but with incorrect results so far.

 XmlDocument XmlDoc = new XmlDocument();
 XmlDoc.Load(Server.MapPath("~/Upload/" + FileUpload1.FileName));

 XmlNodeList question = XmlDoc.GetElementsByTagName("row");

 foreach(XmlNode Node in question)
 {
      string answer = Node["var"].Attributes["name"].InnerText;
      string ques = Node["var"].InnerText;

      DropDownList1.Items.Add(new ListItem(answer, ques));
 }

Here is my xml file

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<root>
  <row>
    <var name="Name" value="Garcia" />
    <var name=" Surname" value=" Jose" />
    <var name=" Country" value=" Cuba" />
    <var name=" Job" value="Software Developer" />
    <var name=" Cabin" value="345A" />
    </row>
    <row>
    <var name="Name" value="Lenon" />
    <var name=" Surname" value="Tim" />
    <var name=" Country" value="USA" />
    <var name=" Job" value="SoftwareDeveloper" />
    <var name=" Cabin" value="444" />
    </row>
    <row>
    <var name="Name" value="Rusell" />
    <var name=" Surname" value=" Anthony" />
    <var name=" Country" value=" UK" />
    <var name=" Job" value="Web Designer" />
    <var name=" Cabin" value="345" />
  </row>
  <row>
    <var name="Name" value="Wolf" />
    <var name=" Surname" value=" Werner" />
    <var name=" Country" value=" Germany" />
    <var name=" Job" value="Linux IT" />
    <var name=" Cabin" value="234 " />
  </row>
</root>

What I need to do is just populate a drop down list with the values Name,Surname,Country,Job and Cabin,so the user can select these values to manipulate the data. I realise with the answer tag im accessing the values also I was trying different things from code ive saw.

The results im getting in my dropdown list from this code is Name Name Name Name Im adding the first attribute of each node, but what I need to do is add every value from just one node. NOTE: The xml files il be work开发者_如何学运维ing with will have different values and names etc so hardcoding is not an option.

If anyone could help Id appreciate it, thank you.


XDocument xml = XDocument.Load(Server.MapPath("~/Upload/" + FileUpload1.FileName));

foreach (var el in xml.Document.Descendants().First().Descendants().First().Descendants())
{
    DropDownList1.Items.Add(new ListItem(el.Attribute(XName.Get("name")).Value, Value = el.Value));
}


This is something that you should use DataBinding to accomplish. This blog post has a good example of how you would do this for ASP.NET, which would also apply to WinForms. The key function is:

//populates the dropdownlist from xml file
public void PopulateDDLsFromXMLFile()
{
    DataSet ds = new DataSet();
    ds.ReadXml(MapPath("~/Resources/XMLFile.xml"));

    //now define datatext field and datavalue field of dropdownlist
    ddlName.DataTextField = "Name";
    ddlName.DataValueField = "Name";
    ...

    //now bind the dropdownlist to the dataview
    ddlName.DataSource = ds;
    ddlName.DataBind();
}

For some more information on databinding, you could read the following: ASP.NET, WinForms, WPF.


I'm not exactly sure what you're doing with the drop down list, but you can use the following code to get to the individual values you want:

    XmlDocument XmlDoc = new XmlDocument();
    XmlDoc.Load(Server.MapPath("~/Upload/" + FileUpload1.FileName));

    string searchpath = "//root//row";
    XmlNodeList xmlnodes = XmlDoc.SelectNodes(searchpath);

    foreach (XmlNode node in xmlnodes)
    {
         string name = node.SelectSingleNode("//var[@name='Name']").Attributes["value"].InnerXml;
         string surname = node.SelectSingleNode("//var[@name=' Surname']").Attributes["value"].InnerXml;
         string Country = node.SelectSingleNode("//var[@name=' Country']").Attributes["value"].InnerXml;
         string Job = node.SelectSingleNode("//var[@name=' Job']").Attributes["value"].InnerXml;
         string Cabin = node.SelectSingleNode("//var[@name=' Cabin']").Attributes["value"].InnerXml;
    }


Are you saying that you want to show Garcia, Lenon,Rusell and Wolf in the ddl? if so, just change your :

Node["var"].Attributes["name"].InnerText;

to

Node["var"].Attributes["value"].Value;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜