I'm trying to add the country web service to a dropdownlist. I have added the web reference and have the discomap and wsdl files.
Here's my cod开发者_开发技巧e-behind:
net.webservicex.www.country ws = new net.webservicex.www.country();
ddlCountry.DataSource = ws.GetCountries();
ddlCountry.DataBind();
I tried the above code, but it only displays one character per line in the dropdownlist. I'm not really sure how to do this, this is my first time using web services. I appreciate any help. Thanks!
GetCountries() returns XML. You need to parse XML to get the list of countries as a list of strings.
If you have .NET 3.5 or higher, you can do this easily with LINQ to XML.
using System;
using System.Linq;
using System.Xml.Linq;
namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
var service = new net.webservicex.www.country();
var xml = service.GetCountries();
var countries = XDocument.Parse(xml).Descendants("Name").Select(arg => arg.Value).ToList();
countriesDropDownList.DataSource = countries;
countriesDropDownList.DataBind();
}
}
}
精彩评论