1

我有这段代码

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 (category.name == "Featured")从未调用过 if 语句,我不确定这是为什么。开始解析 xpath 并将它们的链接存储到一个数组中。感谢您提供的任何帮助。

4

1 回答 1

-2

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

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

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

于 2013-03-26T09:47:39.150 回答