C# html agility pack对象引用未设置为对象的实例
我使用html Agility Pack等东西来解析html 但我遇到了一些不好的事情:| 这是我的背景代码C# html agility pack对象引用未设置为对象的实例,c#,asp.net-web-api,html-agility-pack,C#,Asp.net Web Api,Html Agility Pack,我使用html Agility Pack等东西来解析html 但我遇到了一些不好的事情:| 这是我的背景代码 public static HtmlDocument GetXHtmlFromUri2(string uri) { HttpClient client = HttpClientFactory.Create(new CustomeHeaderHandler()); var htmlDoc = new HtmlDocument()
public static HtmlDocument GetXHtmlFromUri2(string uri)
{
HttpClient client = HttpClientFactory.Create(new CustomeHeaderHandler());
var htmlDoc = new HtmlDocument()
{
OptionCheckSyntax = true,
OptionFixNestedTags = true,
OptionAutoCloseOnEnd = true,
OptionReadEncoding = true,
OptionDefaultStreamEncoding = Encoding.UTF8,
};
htmlDoc.LoadHtml(client.GetStringAsync(uri).Result);
return htmlDoc;
}
我对WebApi Mvc4使用html敏捷性,这是Get方法逻辑
//GET api/values
public string GetHtmlFlights()
{
var result = ClientFlightTabale.GetXHtmlFromUri2("http://ikiafids.ir/departureFA.html");
HtmlNode node = result.DocumentNode.SelectSingleNode("//table[1]/tbody/tr[1]");
string temp = node.FirstChild.InnerHtml.Trim();
return temp;
}
但当我从浏览器调用此方法时,Fiddler遇到了异常,主题如下:
对象引用未设置为对象的实例,此异常与此行有关
string temp = node.FirstChild.InnerHtml.Trim();
有人能帮我吗?我想你的选择器错了。试试这个
result.DocumentNode.SelectSingleNode("//table/tr[1]")
我认为你的选择器错了。试试这个
result.DocumentNode.SelectSingleNode("//table/tr[1]")
我想你正在寻找这样的东西:
var result = ClientFlightTabale.GetXHtmlFromUri2("http://ikiafids.ir/departureFA.html");
var tableNode = result.DocumentNode.SelectSingleNode("//table[1]");
var titles = tableNode.Descendants("th")
.Select(th => th.InnerText)
.ToList();
var table = tableNode.Descendants("tr").Skip(1)
.Select(tr => tr.Descendants("td")
.Select(td => td.InnerText)
.ToList())
.ToList();
我想你正在寻找这样的东西:
var result = ClientFlightTabale.GetXHtmlFromUri2("http://ikiafids.ir/departureFA.html");
var tableNode = result.DocumentNode.SelectSingleNode("//table[1]");
var titles = tableNode.Descendants("th")
.Select(th => th.InnerText)
.ToList();
var table = tableNode.Descendants("tr").Skip(1)
.Select(tr => tr.Descendants("td")
.Select(td => td.InnerText)
.ToList())
.ToList();
如何将任何td属性指定给特定属性?td.Attributes[someattribute].Valuesry但我希望将每个元素td分配给一个属性,如Model.flightId如何将任何td属性指定给特定属性?td.Attributes[someattribute].Valuesry但我希望将每个元素td分配给一个属性,如Model.FlightID