如果从未调用过的语句

时间:2013-03-26 09:37:21

标签: c# html-agility-pack

我有这段代码

Category featured = new Category()
            {
                active = true,
                Categories = new Category[0],
                description = "",
                identifier = "featured",
                name = "Featured",
                Products = new Product[0],
                url = siteUrl,
            };
StatisticCounters.CategoriesCounter();

然后在这下面我有这个代码。

private IList<Category> FeatureSubCategories(HtmlNode std, Category category,Category featured)
{
    List<Category> categories = new List<Category>();
    {
        if (category.name == "Featured")
        {
            var nodes = std.SelectNodes("//span[contains(@class,'widget')] [position() <= 4]"); //TODO

            foreach (var node in nodes)
            {
                string name = SiteParserUtilities.ParserUtilities.CleanText(System.Net.WebUtility.HtmlDecode(node.InnerText));
                string url = node.Attributes["href"].Value;
                string identifier = url.Split('/').Last().Replace(".html", "");
                WriteQueue.write(string.Format(" Category [{0}].. {1} ", name, url));

                IList<Category> sub = GetSubCategories(std);
                Category c = new Category()
                {
                    active = true,
                    Categories = sub.ToArray(),
                    description = "",
                    identifier = identifier,
                    name = name,
                    Products = new Product[0],
                    url = url,
                };
                StatisticCounters.CategoriesCounter();
                categories.Add(c);
            }
        }
    }
    return categories;
}

由于某种原因,当我运行代码时if语句if (category.name == "Featured")从未被调用,我不知道为什么会这样。开始解析xpath并将链接存储到数组中。感谢您提供任何帮助。

1 个答案:

答案 0 :(得分:-2)

最好检入调试器。有时白色空间会导致问题。许多程序员都犯了一个简单的错误。当我们从数据库获取数据时,可能会有一些前导空格。当我们与某个值进行比较时(认为没有前导空格),如果不修剪这些空间,它将无法进行比较。例如

string s1 = "TEST"; 
string s2 = " TEST"; 
if (s1 == s2) { 
    Console.WriteLine("Equal"); 
}

请使用修剪功能删除任何空格并进行比较。