删除XMLNode中重复项的最佳方法是什么

时间:2011-09-19 01:09:58

标签: c# asp.net xml

删除XMLNode中重复项的最佳方法是,下面是我的代码。

XmlDocument xdoc = new XmlDocument();//xml doc used for xml parsing

    xdoc.Load(url);

XmlNamespaceManager nsmgr = new XmlNamespaceManager(xdoc.NameTable);
nsmgr.AddNamespace("content", "sitename.xsd");

var topicNodes = xdoc.SelectNodes("//content:Topic", nsmgr);

foreach (XmlNode node in topicNodes)
{
    string topic = node.Attributes["TopicName"].Value;
    //adding topic name to dropdwonlist
    dropdownlist.items.add(new listitem(topic);
}
dropdownlist.databind();

1 个答案:

答案 0 :(得分:1)

一个选项是创建一个HashSet,当你循环检查是否在HashSet中......如果没有,添加到HashSet和下拉列表。

你会使用HashSet - 这是Add and Contains:

http://msdn.microsoft.com/en-us/library/bb356440.aspx