tags:

views:

35

answers:

1

I have the following xml file:

<contract>
    <ID>4</ID>
    <name>Name of contract</name>
    <commoditycode>CS,CP</commoditycode>
</contract>

I want to have the comma separated values in "commoditycode" in a dropdown list like this:

<asp:DropDownList ID="DropDownList1" runat="server">
    <asp:ListItem Text="CS" Value="CS" />
    <asp:ListItem Text="CP" Value="CP" />
</asp:DropDownList>

in order to filter my list of contract. Is this possible? Many thanks!

+1  A: 

You could definitely load the XML file into a XML Document:

XmlDocument doc = new XmlDocument();
doc.Load('your-xml-file-name.xml');

and then get the value of the commodity code node:

XmlNode nodeCommCode = doc.SelectSingleNode("/contract[ID='4']/commoditycode");

string commodityCodeValue = string.Empty;

if(nodeCommCode != null)
{
   commodityCodeValue = nodeCommCode.InnerText;
}

and then split that string into an arry of strings:

string[] elements = commodityCodeValue.Split(",");

and then add each of those elements to the ASP.NET drop down list:

foreach(string oneElement in elements)
{  
    ddlYourDropDown.Items.Add(oneElement);
}

That should do it :-)

marc_s
many thanks for this.
aspNetAficionado