0

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;
slugster
  • 49,403
  • 14
  • 95
  • 145
Matt McCarthy
  • 115
  • 1
  • 3
  • 12

1 Answers1

2

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.

nine9ths
  • 796
  • 6
  • 15