c# - 将 ASCII 编码为 HTML

标签 c# html xml ascii webclient

我正在尝试 DownloadData 方法来自 WebClient 。我当前的问题是我无法弄清楚如何转换 ASCII result ( &lt;<\n&gt;> )由 Encoding.ASCII.GetString(myDataBuffer); 生成,出此page .

pagesource
(来源:iforce.co.nz)

    /// <summary>
    /// Curl data from the PMID
    /// </summary>
    private void ClientPMID(int pmid)
    {
        //generate the URL for the client
        StringBuilder pmid_url_string = new StringBuilder();
        pmid_url_string.Append("http://www.ncbi.nlm.nih.gov/pubmed/").Append(pmid.ToString()).Append("?report=xml");
        Uri PMIDUri = new Uri(pmid_url_string.ToString());
        //declare and initialize the client
        WebClient client = new WebClient();
        // Download the Web resource and save it into a data buffer. 
        byte[] myDataBuffer = client.DownloadData(PMIDUri);
        this.DownloadCompleted(myDataBuffer);
    }
    /// <summary>
    /// Crawl over the binary from myDataBuffer
    /// </summary>
    /// <param name="myDataBuffer">Binary Buffer</param>
    private void DownloadCompleted(byte[] myDataBuffer)
    {
        string download = Encoding.ASCII.GetString(myDataBuffer);
        PMIDCrawler pmc = new PMIDCrawler(download, "/pre/PubmedArticle/MedlineCitation/Article");
        //iterate over each node in the file
        foreach (XmlNode xmlNode in pmc.crawl)
        {
            string AbstractTitle = xmlNode["ArticleTitle"].InnerText;
            string AbstractText = xmlNode["Abstract"]["AbstractText"].InnerText;
        }
    }

PMIDCrawler 的代码可在我关于 DownloadStringCompletedEventHandler 的其他问题中找到。 。虽然输出来自 string html = HttpUtility.HtmlDecode(nHtml);无效 HTML (OR XML) (由于它没有响应 xml http header ),在收到来自 Encoding.ASCII.GetString 的内容后.

最佳答案

不幸的是,该服务器无法正确响应接受:text/xml接受:application/xml,因此您必须以困难的方式完成此操作(HttpUtility)

string download = HttpUtility.HtmlDecode(Encoding.ASCII.GetString(myDataBuffer));

(或 .NET Fx 4.5+ 上的 WebUtility.Decode)

string download = Encoding.ASCII.GetString(myDataBuffer);
if (download != null) { // this won't get all HTML escaped characters...
    download = download.Replace("&lt;", "<").Replace("&gt;", ">");
}

另请参阅 this question 了解更多信息。

关于c# - 将 ASCII 编码为 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15376101/

相关文章:

c# - 如何仅使用跟踪的更改来压缩文件?

c# - 在 MySql (PhpMyadmin) 中根据条件将数据插入字段中

javascript - 当前url作为jquery的输入值

javascript - 我可以用类将图像添加到 div 中吗?

Python minidom 从 XML 中提取文本

html - XPath浮点除法?

c# - 在 C# 中使用 XDocument 创建 XML 文件

c# - 线程传递数据和窗口关闭

javascript - Ionic 框架使 'open in new tab' 选项对 ios 设备中的 ng-href 不可用

c# - 在 javascript 中创建 C# 对象