Created
June 26, 2017 01:00
-
-
Save KageShiron/c83da63ec87ac59e802fbaf734138958 to your computer and use it in GitHub Desktop.
blog/xamarin-hap-withxpath : XamarinとHtmlAgilityPack
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// LINQ | |
var items = htmlDoc.DocumentNode.Descendants("div") | |
.Where(e => e.GetAttributeValue("class", "") == "resultItem"); | |
SearchResults = items.Select(r => { | |
string kind=r.Descendants("div").Where(e=>e.GetAttributeValue("class", "")=="resultKind") | |
.FirstOrDefault()?.InnerText; | |
string name=r.Descendants("div").Where(e=>e.GetAttributeValue("class","")=="resultName") | |
.FirstOrDefault()?.InnerText; | |
// XPath | |
doc.DocumentNode.SelectNodes("//div[@class='resultItem']")?.Select(r =>{ | |
string kind = r.SelectSingleNode("div/div[@class='resultKind']")?.InnerText; | |
string name = r.SelectSingleNode("div/div[@class='resultName']")?.InnerText; | |
}); | |
// CSS Selector(未実装) | |
doc.DocumentNode.CSSセレクタメソッド("div.resultItem")?.Select(r =>{ | |
string kind = r.CSSセレクタメソッド("div div.resultKind")?.InnerText; | |
string name = r.CSSセレクタメソッド("div div.resultName")?.InnerText; | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TypeSearchResult | |
{ | |
public Uri Uri { get; set; } | |
public Uri Image { get; set; } | |
public string Kind { get; set; } | |
public string Name { get; set; } | |
public string FullName { get; set; } | |
} | |
public async Task<IEnumerable<TypeSearchResult>> Search(string symbol) | |
{ | |
HtmlWeb web = new HtmlWeb(); | |
//Reference SourceのWebサイトから検索 | |
HtmlDocument doc = await web.LoadFromWebAsync("https://referencesource.microsoft.com/api/symbols/?symbol=" + symbol); | |
return doc.DocumentNode.SelectNodes("//div[@class='resultItem']")?.Select(r => | |
{ | |
string kind = r.SelectSingleNode("div/div[@class='resultKind']")?.InnerText; | |
string name = r.SelectSingleNode("div/div[@class='resultName']")?.InnerText; | |
return new TypeSearchResult() | |
{ | |
Uri = new Uri("https://referencesource.microsoft.com" + r.ParentNode.GetAttributeValue("href", "")), | |
Image = new Uri("https://referencesource.microsoft.com" + | |
r.SelectSingleNode("div[@class='resultLine']/img")?.GetAttributeValue("src", "")), | |
Kind = kind, | |
Name = name, | |
FullName = r.SelectSingleNode("div[@class='resultDescription']")?.InnerText | |
}; | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment