好的,我会尽量快速和简单地解释这一点......
我想要做的是从 xml 中提取四种不同的东西......首先是我正在使用的 XML 的 url,我试图从那个 url XML 中只显示名称(符号)、Last、High和低。
因此,在我的应用程序中,当用户点击按钮获取股票报价时,现在出现的是来自该 XML 的所有内容,但我只想显示上面列出的那 4 件事。
这是我现在的代码...
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;