【问题标题】:How to turn url into XML如何将 url 转换为 XML
【发布时间】:2012-11-11 07:16:46
【问题描述】:

我正在尝试使用此网址...

http://www.webservicex.net/stockquote.asmx/GetQuote?Symbol=T

它的作用类似于 XML,但格式不正确,我希望能够使用它以 XML 形式显示它...

这是我现在的代码

protected void btnGetResult_Click(object sender, EventArgs e)
{
    XPathNavigator nav;
    XmlDocument myXMLDocument = new XmlDocument();
    String stockQuote = "http://www.webservicex.net/stockquote.asmx/GetQuote?Symbol=T" + txtInfo.Text;
    myXMLDocument.Load(stockQuote);

    // Create a navigator to query with XPath.
    nav = myXMLDocument.CreateNavigator();
    nav.MoveToRoot();
    nav.MoveToFirstChild();

    do
    {
        //Find the first element.
        if (nav.NodeType == XPathNodeType.Element)
        {
            //Move to the first child.
            nav.MoveToFirstChild();

            //Loop through all the children.
            do
            {
                //Display the data.
                txtResults.Text = txtResults.Text + nav.Name + " - " + nav.Value + Environment.NewLine;
            } while (nav.MoveToNext());
        }
    } while (nav.MoveToNext());
}

【问题讨论】:

    标签: c# xml web-services


    【解决方案1】:

    查看您收到的回复的来源。内容(带有根节点的所有内容)不是 XML。标签是 HTML 实体:<>。将其直接加载到您的 XmlDocument 中会将您所追求的所有内容视为简单文本。

    以下示例下载在线资源,用标签替换 HTML 实体并重新加载 XML 文档,然后 XPath 可以访问该文档。

    string url = "http://www.webservicex.net/stockquote.asmx/GetQuote?Symbol=T";
    
    XmlDocument doc = new XmlDocument();
    doc.Load(url);
    
    string actualXml = doc.OuterXml;
    actualXml = actualXml.Replace("&lt;", "<");
    actualXml = actualXml.Replace("&gt;", ">");
    doc.LoadXml(actualXml);
    

    我没有考虑您示例源代码的第二部分。但我想拥有一个合适的 XML 文档将是第一步。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-02
      • 2011-01-20
      • 2020-11-14
      • 2015-01-13
      • 2018-03-28
      • 2016-06-04
      • 2023-04-10
      • 2014-04-23
      相关资源
      最近更新 更多