如何从XML文档中提取特定数据 [英] How to extract certain data from a XML document

查看:327
本文介绍了如何从XML文档中提取特定数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我会尽力解释这个快速和简单,只要我能...

我所试图做的是从XML中提取四个不同的东西......第一关的这里是我使用XML的网址,并从该网址XML我想只显示名称(符号),最后,高和低。

所以,在我的应用程序时,用户点击按钮获取股票报价显示的内容,现在是从XML的一切,但我只希望这些4件事我上面列出露面。

下面是我的code,我现在...

  HttpWebRequest的myHttpWebRequest = NULL; //声明WebRequest类的HTTP特定的实现。
     HttpWebResponse myHttpWebResponse = NULL; //声明提供WebResponse类的HTTP特定的实现
     XmlTextReader的myXMLReader = NULL; //声明的XMLReader
     XPathNavigator的资产净值;
     XPathDocument中docNav;     //创建请求
     字符串的StockQuote =htt​​p://www.webservicex.net/stockquote.asmx/GetQuote?Symbol=T+ txtInfo.Text;
     myHttpWebRequest =(HttpWebRequest的)HttpWebRequest.Create(股票报价);
     myHttpWebRequest.Method =GET;
     myHttpWebRequest.ContentType =文本/ XML;编码='UTF-8';
     //获取响应
     myHttpWebResponse =(HttpWebResponse)myHttpWebRequest.GetResponse();     //加载响应流进的XMLReader
     myXMLReader = XmlTextReader的新(myHttpWebResponse.GetResponseStream());     docNav =新的XPathDocument(myXMLReader);
     //创建一个导航仪使用XPath查询。
     NAV = docNav.CreateNavigator();
     txtResults.Text = txtResults.Text + nav.Name + - + nav.Value + Environment.NewLine;


解决方案

的问题是,服务没有实际提供的XML饲料,它只是周围的一些连接,看起来像XML codeD数据的XML包装。你将不得不preprocess它之前,你可以使用XPath访问任何元素出来。你可以看到这个,如果你xmllint它

 %xmllint --format'http://www.webservicex.net/stockquote.asmx/GetQuote?Symbol=T
< XML版本=1.0编码=UTF-8&GT?;
<字符串xmlns=\"http://www.webserviceX.NET/\"><StockQuotes><Stock><Symbol>T</Symbol><Last>33.54</Last><Date>11/9/2012</Date><Time>4:00pm</Time><Change>+0.34</Change><Open>33.02</Open><High>33.72</High><Low>32.71</Low><Volume>31871880</Volume><MktCap>190.5B</MktCap><$p$pviousClose>33.20</$p$pviousClose><PercentageChange>+1.02%</PercentageChange><AnnRange>27.41 - 38.58</AnnRange><Earns>0.756</Earns><P-E>43.92</P-E><Name>AT&T公司与放大器; LT; /名称和放大器; GT;&放大器; LT; /股票及放大器; GT;&放大器; LT; / StockQuotes和放大器; GT;< /串>

我不是一个.NET程序员,所以我不能给你一个例证答案,但是从我读到的 的XmlTextReader 它看起来像它的走向读取数据这已经面向XML。然而,这种反应是XML的唯一部分是<串> 元素,它的其余部分已逃出外,< > 转换为&放大器; LT; &安培; GT; 分别,这使得它只是一堆文字

使用我15在这一点.NET体验整个分钟,它看起来对我来说,你需要将这些转换回(不知道这样做的最好的做法是什么),然后可能使用类似的 的loadXML ,你可以实际使用的XmlTextReader 在它之前。请相应地放大盐粮你用这个建议。

或者你可以尝试并获得无论是谁提供这种服务实际上发出真正的XML。

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.

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.

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. You would have to preprocess it before you could use xpath to access any elements out of it. You can see this if you xmllint it

%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. 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; and &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. 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文档中提取特定数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆