带有 IF 语句的 C# HTML Agility Pack
我有这段代码,我需要运行 if 语句来设置变量的值。问题是当第一个条件为空时它会失败。谁能告诉我我做错了什么?
IF 语句位于 foreach 循环内部,该循环在每次迭代时向列表添加一个值。
非常感谢!
string result = string.Empty;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
using (var stream = request.GetResponse().GetResponseStream())
using (var reader = new StreamReader(stream, Encoding.UTF8))
{
result = reader.ReadToEnd();
}
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.Load(new StringReader(result));
HtmlNode root = doc.DocumentNode;
string itemdesc = doc.DocumentNode.SelectSingleNode("//h1[@class='producttitle']").InnerText;
HtmlNodeCollection nodes = doc.DocumentNode.SelectNodes("//div[@class='resultsset']/table/tbody[@class='result']/tr");
List<string> sellers = new List<string>();
List<string> prices = new List<string>();
foreach (HtmlNode node in nodes)
{
string seller = string.Empty;
if(node.SelectSingleNode(".//ul[@class='sellerInformation']/img").GetAttributeValue("alt", string.Empty) != null)
{
seller = node.SelectSingleNode(".//ul[@class='sellerInformation']/img").GetAttributeValue("alt", string.Empty);
}
else if (node.SelectSingleNode(".//ul[@class='sellerInformation']/a/img").GetAttributeValue("alt", string.Empty) != null)
{
seller = node.SelectSingleNode(".//ul[@class='sellerInformation']/a/img").GetAttributeValue("alt", string.Empty);
}
else
{
seller = node.SelectSingleNode(".//ul[@class='sellerInformation']/li/div/span/a/b").InnerText;
}
sellers.Add(seller);
string price = node.SelectSingleNode(".//span[@class='price']").InnerText;
prices.Add(price);
}
I have this code that I need to run an if statement to set the value of a variable. The problem is when the first condition is null it fails. Can anyone tell me what I am doing wrong?
The IF statement is inside the foreach loop which adds a value to a list on each iteration.
Thank you very much!
string result = string.Empty;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
using (var stream = request.GetResponse().GetResponseStream())
using (var reader = new StreamReader(stream, Encoding.UTF8))
{
result = reader.ReadToEnd();
}
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.Load(new StringReader(result));
HtmlNode root = doc.DocumentNode;
string itemdesc = doc.DocumentNode.SelectSingleNode("//h1[@class='producttitle']").InnerText;
HtmlNodeCollection nodes = doc.DocumentNode.SelectNodes("//div[@class='resultsset']/table/tbody[@class='result']/tr");
List<string> sellers = new List<string>();
List<string> prices = new List<string>();
foreach (HtmlNode node in nodes)
{
string seller = string.Empty;
if(node.SelectSingleNode(".//ul[@class='sellerInformation']/img").GetAttributeValue("alt", string.Empty) != null)
{
seller = node.SelectSingleNode(".//ul[@class='sellerInformation']/img").GetAttributeValue("alt", string.Empty);
}
else if (node.SelectSingleNode(".//ul[@class='sellerInformation']/a/img").GetAttributeValue("alt", string.Empty) != null)
{
seller = node.SelectSingleNode(".//ul[@class='sellerInformation']/a/img").GetAttributeValue("alt", string.Empty);
}
else
{
seller = node.SelectSingleNode(".//ul[@class='sellerInformation']/li/div/span/a/b").InnerText;
}
sellers.Add(seller);
string price = node.SelectSingleNode(".//span[@class='price']").InnerText;
prices.Add(price);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可能
SelectSingleNode
返回 null,因此对GetAttributeValue
的调用是一个 null 引用问题。在检查属性之前,您需要检查SelectSingleNode
的结果。Probably
SelectSingleNode
is returning null, so the call toGetAttributeValue
is a null reference problem. You will need to check the result ofSelectSingleNode
before checking the attribute.