1

なぜ myWebClientがアクセスしていないのか、私は混乱していDownloadStringCompletedます。WebClientで発生する可能性のある問題を読んだ後disposed、ダウンロードを完了する前に. または、exceptions中に捕らえられていないDownloadDataか、単にUriアクセスできない.

私はこれらすべての問題をチェックしましたが、私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 NodeList コンストラクター クラス

    /// <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なぜに到達しないのかについてのアイデアはありますか?

4

1 に答える 1

1

コードにいくつかの問題がありますCurlPMID。以下のコードにコメントを入れました。

public void CurlPMID()
{
    // 1. The variable 'client' loses scope when this function exits.
    //    You may want to consider making it a class variable, so it doesn't
    //    get disposed early.
    WebClient client = new WebClient();
    client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(HttpsCompleted);
    // 2. You are calling the synchronous version of the download function.
    //    The synchronous version does not call any completion handlers.
    //    When the synchronous call returns, the download has completed.
    // 3. You are calling the wrong function here.  Based on your completion handler, 
    //    you should be calling DownloadStringAsync(). If you want synchronous 
    //    behavior, call DownloadString() instead.
    client.DownloadData(this.pmid_url);
}

つまり、非同期動作が必要だとすると、CurlPMID関数は次のようになります。

public void CurlPMID()
{
    WebClient client = new WebClient();
    client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(HttpsCompleted);
    client.DownloadStringAsync(this.pmid_url);
}
于 2013-03-13T01:09:49.717 に答える