LinkedInから抽出したデータをグリッドに入力しようとしていますが、自分の学習曲線で機能させるようにしていますが、線を削除すると
MessageBox.Show("asdfasdfasdf")
リスト「メッセージ」には1つの項目しかありません。上の行を含めると、期待どおりの結果が得られ、15個のメッセージが表示されます。
誰かが説明できますか?
public void extract_messages_received(object sender, RoutedEventArgs e)
{
triggered = false;
System.Windows.Forms.WebBrowser browser = new System.Windows.Forms.WebBrowser();
browser.Navigate(new Uri(@"http://www.linkedin.com/inbox/messages/received"));
browser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(browser_DocumentCompleted);
}
private void LoadMessages(string url)
{
txtOutput.Text = @"http://www.linkedin.com" + url.Substring(6, url.Length - 6);
if (!urls.Contains(url))
{
urls.Add(url);
WebBrowser browser = new WebBrowser();
browser.Navigate(new Uri(txtOutput.Text);
loaded_message = false;
browser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(ReadMessages);
}
}
private void ReadMessages(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (loaded_message == false)
{
string url = ((WebBrowser)sender).Url.ToString();
int loc1 = url.IndexOf("itemID") + 7;
int loc2 = url.IndexOf("&", loc1);
IEnumerable<string> name = null;
IEnumerable<string> odate = null;
IEnumerable<string> photo = null;
IEnumerable<string> subject = null;
IEnumerable<string> headline = null;
string body = "";
string id = url.Substring(loc1, loc2 - loc1);
//System.Windows.MessageBox.Show("READ");
foreach (HtmlElement element in ((WebBrowser)sender).Document.GetElementsByTagName("div"))
{
if (element.GetAttribute("classname").Equals("inbox-item-body"))
{
body = element.InnerText;
}
if (element.GetAttribute("classname").Equals("inbox-item-header"))
{
var doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(element.InnerHtml);
name = from foo in doc.DocumentNode.SelectNodes("//a[@class='fn']") select foo.InnerText;
odate = from foo in doc.DocumentNode.SelectNodes("//p[@class='date']") select foo.InnerText;
photo = from foo in doc.DocumentNode.SelectNodes("//img[@class='photo']") select foo.Attributes["src"].Value;
subject = from foo in doc.DocumentNode.SelectNodes("//h3") select foo.InnerText;
headline = from foo in doc.DocumentNode.SelectNodes("//span[@class='headline']") select foo.InnerText;
}
}
// ****
MessageBox.Show("asdfasdfasdf");
// ****
messages.Add(new Messages()
{
ID = id,
Subject = subject.First().ToString(),
Headline = headline.First().ToString(),
Sender = name.First().ToString(),
Photo = photo.First().ToString(),
SendDate = odate.First().ToString(),
Body = body
});
// dataMessages.ItemsSource = messages;
}
loaded_message = true;
}
void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (!triggered)
{
triggered = true;
System.Windows.Forms.WebBrowser web = sender as System.Windows.Forms.WebBrowser;
foreach (HtmlElement element in web.Document.GetElementsByTagName("ol"))
{
if (element.GetAttribute("classname").Contains("inbox-list "))
{
WebBrowser browser = new WebBrowser();
browser.Navigate("about:blank");
browser.Document.Write(element.InnerHtml);
HtmlElementCollection hrefTags = null;
hrefTags = browser.Document.GetElementsByTagName("a");
foreach (HtmlElement a in hrefTags)
{
if (a.OuterHtml.Contains("displayMBox"))
{
LoadMessages(a.GetAttribute("href"));
}
}
}
}
}
}