How do I display distinct items in the autocomplete list? I don’t want the duplicates to show up in the list. I’m reading the data from an XML file.
Here’s my web service code:
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class WebService : System.Web.Services.WebService
{
[WebMethod]
public string[] GetItemsList(string prefixText, int count)
{
List<string> suggestions = new List<string>();
using (XmlTextReader reader = new XmlTextReader(HttpContext.Current.Server.MapPath("flightdata3.xml")))
{
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element && reader.Name == "departurelocation")
{
string itemName = reader.ReadInnerXml();
if (itemName.StartsWith(prefixText, StringComparison.InvariantCultureIgnoreCase))
{
suggestions.Add(itemName);
if (suggestions.Count == count) break;
}
}
if (reader.NodeType == XmlNodeType.Element && reader.Name == "destinationlocation")
{
string itemName = reader.ReadInnerXml();
if (itemName.StartsWith(prefixText, StringComparison.InvariantCultureIgnoreCase))
{
suggestions.Add(itemName);
if (suggestions.Count == count) break;
}
}
}
}
return suggestions.ToArray();
}
}
To guarantee a count you would have to check for distinct items before adding them:
Otherwise you end up worst case with only one suggestion.