简体   繁体   English

如何从XML文档中提取某些数据

[英]How to extract certain data from a XML document

Okay I'll try to explain this quick and as simple as I can... 好的,我会尽快解释这个,尽可能简单...

What I am trying to do is extract four different things from a xml... first off here is the url of the XML I am using, and from that url XML I am trying to display only the Name(Symbol), Last, High, and Low. 我想要做的是从xml中提取四个不同的东西......首先这里是我正在使用的XML 的URL ,并且从那个url XML我试图只显示名称(符号),Last,High和低。

So on my application when the user hits the button to get a stock quote what appears right now is everything from that XML, but I only want those 4 things I listed above to show up. 因此,在我的应用程序中,当用户点击按钮获取股票报价时,现在出现的是来自该XML的所有内容,但我只希望显示上面列出的这4项内容。

Here is my code that I right now... 这是我现在的代码......

     HttpWebRequest myHttpWebRequest = null;     //Declare an HTTP-specific implementation of the WebRequest class.
     HttpWebResponse myHttpWebResponse = null;   //Declare an HTTP-specific implementation of the WebResponse class
     XmlTextReader myXMLReader = null;           //Declare XMLReader           
     XPathNavigator nav;
     XPathDocument docNav;

     //Create Request
     String stockQuote = "http://www.webservicex.net/stockquote.asmx/GetQuote?Symbol=T" + txtInfo.Text;


     myHttpWebRequest = (HttpWebRequest)HttpWebRequest.Create(stockQuote);
     myHttpWebRequest.Method = "GET";
     myHttpWebRequest.ContentType = "text/xml; encoding='utf-8'";
     //Get Response
     myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();

     //Load response stream into XMLReader
     myXMLReader = new XmlTextReader(myHttpWebResponse.GetResponseStream());

     docNav = new XPathDocument(myXMLReader);
     // Create a navigator to query with XPath.
     nav = docNav.CreateNavigator();


     txtResults.Text = txtResults.Text + nav.Name + " - " + nav.Value + Environment.NewLine;

The problem is that service is not actually providing an xml feed, it's just an xml wrapper around some encoded data that looks like xml. 问题是服务实际上并没有提供xml提要,它只是一些xml包装器,它围绕着一些看起来像xml的编码数据。 You would have to preprocess it before you could use xpath to access any elements out of it. 在使用xpath访问其中的任何元素之前,您必须预先处理它。 You can see this if you xmllint it 你可以看到这个xmllint它

%xmllint --format 'http://www.webservicex.net/stockquote.asmx/GetQuote?Symbol=T'
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://www.webserviceX.NET/">&lt;StockQuotes&gt;&lt;Stock&gt;&lt;Symbol&gt;T&lt;/Symbol&gt;&lt;Last&gt;33.54&lt;/Last&gt;&lt;Date&gt;11/9/2012&lt;/Date&gt;&lt;Time&gt;4:00pm&lt;/Time&gt;&lt;Change&gt;+0.34&lt;/Change&gt;&lt;Open&gt;33.02&lt;/Open&gt;&lt;High&gt;33.72&lt;/High&gt;&lt;Low&gt;32.71&lt;/Low&gt;&lt;Volume&gt;31871880&lt;/Volume&gt;&lt;MktCap&gt;190.5B&lt;/MktCap&gt;&lt;PreviousClose&gt;33.20&lt;/PreviousClose&gt;&lt;PercentageChange&gt;+1.02%&lt;/PercentageChange&gt;&lt;AnnRange&gt;27.41 - 38.58&lt;/AnnRange&gt;&lt;Earns&gt;0.756&lt;/Earns&gt;&lt;P-E&gt;43.92&lt;/P-E&gt;&lt;Name&gt;AT&amp;T Inc.&lt;/Name&gt;&lt;/Stock&gt;&lt;/StockQuotes&gt;</string>

I'm not a .net programmer, so I won't be able to give you a illustrative answer, but from what I'm reading about XmlTextReader it looks like it's geared toward reading data that's already XML. 我不是.net程序员,所以我无法给你一个说明性的答案,但从我正在阅读的关于XmlTextReader内容来看,它看起来像是在寻找已经是XML的数据。 However, the only part of that response that is XML is the <string> element, the rest of it has been escaped, the < and > converted to &lt; 但是,该响应的唯一部分是XML, <string>元素,其余部分已被转义, <>转换为&lt; and &gt; &gt; respectively, which renders it just a bunch of text. 分别,它只是一堆文字。

With my 15 whole minutes of .net experience at this point, it looks to me like you'd need to convert those back (not sure what the best practice for this would be), then possibly use something like LoadXML before you could actually use XmlTextReader on it. 在这一点上,凭借我15分钟的.net经验,它看起来像你需要将它们转换回来(不确定最佳实践是什么),然后可能在实际使用之前使用类似LoadXML东西XmlTextReader就可以了。 Please appropriately scale the grain of salt you take this advice with. 请适当地调整你采取这种建议的盐粒。

Or you could try and get whoever is providing that service to actually emit real xml. 或者您可以尝试让任何提供该服务的人实际发出真正的xml。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM