【发布时间】:2018-06-10 11:01:52
【问题描述】:
尝试获取 Google 建议。我m getting the XML back but when parsing using XDocument Im 收到以下异常:“根级别的数据无效。”。我不知道是什么原因造成的。
private const string _suggestSearchUrl = "http://www.google.com/complete/search?output=toolbar&q={0}&hl=en";
public List<GoogleSuggestion> GetData(string query)
{
if (String.IsNullOrWhiteSpace(query))
{
throw new ArgumentException("Argument cannot be null or empty!", "query");
}
string result = String.Empty;
using (HttpClient client = new HttpClient())
{
result = String.Format(_suggestSearchUrl, query);
}
XDocument doc = XDocument.Parse(result); (I`m getting exception here)
var suggestions = from suggestion in doc.Descendants("CompleteSuggestion")
select new GoogleSuggestion
{
Phrase = suggestion.Element("suggestion").Attribute("data").Value
};
return suggestions.ToList();
【问题讨论】:
标签: c# search xml-parsing