1
所以我很困惑,爲什麼我的WebClient
沒有訪問它的DownloadStringCompleted
。在讀完WebClient
正在disposed
的可能問題後,才能完成下載。或者exceptions
在DownloadData
期間未被捕獲,或者簡單地Uri
無法訪問。WebClient()永遠不會到達WPF應用程序的DownloadStringCompleted
我檢查了所有這些問題,並且我的WebClient
尚未訪問其DownloadStringCompleted
。
PMID WebClient類
/// <summary>
/// Construct a new curl
/// </summary>
/// <param name="pmid">pmid value</param>
public PMIDCurl(int pmid)
{
this.pmid = pmid;
StringBuilder pmid_url_string = new StringBuilder();
pmid_url_string.Append("http://www.ncbi.nlm.nih.gov/pubmed/").Append(pmid.ToString()).Append("?report=xml");
this.pmid_url = new Uri(pmid_url_string.ToString());
}
/// <summary>
/// Curl data from the PMID
/// </summary>
public void CurlPMID()
{
WebClient client = new WebClient();
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(HttpsCompleted);
client.DownloadData(this.pmid_url);
}
/// <summary>
/// Information to store in the class after the curl
/// </summary>
public string AbstractTitle { get; set; }
public string AbstractText { get; set; }
/// <summary>
/// Retrieve the data from an xml file about a PMID
/// </summary>
/// <param name="sender">System Generated</param>
/// <param name="e">System Generated</param>
private void HttpsCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error == null)
{
PMIDCrawler pmc = new PMIDCrawler(e.Result, "/pre/PubmedArticle/MedlineCitation/Article");
//iterate over each node in the file
foreach (XmlNode xmlNode in pmc.crawl)
{
this.AbstractTitle = xmlNode["ArticleTitle"].InnerText;
this.AbstractText = xmlNode["Abstract"]["AbstractText"].InnerText;
}
}
} //close httpsCompleted
PMID節點列表構造類
/// <summary>
/// List initialized by crawer
/// </summary>
public XmlNodeList crawl { get; set; }
/// <summary>
/// Constructor for the HTML to XML converter
/// </summary>
/// <param name="nHtml"></param>
/// <param name="nodeList"></param>
public PMIDCrawler(string nHtml, string nodeList)
{
//parse it from e
string html = HttpUtility.HtmlDecode(nHtml);
XDocument htmlDoc = XDocument.Parse(html, LoadOptions.None);
//convert the xdocument to an xmldocument
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(htmlDoc.CreateReader());
//load the xmlDocument into a nodelist
XmlElement xmlRoot = xmlDoc.DocumentElement;
this.crawl = xmlRoot.SelectNodes(nodeList);
}
爲什麼永遠達不到DonwloadStringCompleted
任何想法?