I am using an API call to return some XML data from a web server. XML data is in the following format:
<forismatic> <quote> <quoteText>The time you think you're missing, misses you too.</quoteText> <quoteAuthor>Ymber Delecto</quoteAuthor> <senderName></senderName> <senderLink></senderLink> <quoteLink>http://forismatic.com/en/55ed9a13c0/</quoteLink> </quote> </forismatic>
I can get the XML source data successfully, and I want to add the <quoteText> and <quoteAuthor> node <quoteAuthor> to the strings, but it seems unable to do this. My current code is:
private void btnGetQuote_Click(object sender, EventArgs e) { WebRequest req = WebRequest.Create("http://api.forismatic.com/api/1.0/"); req.Method = "POST"; req.ContentType = "application/x-www-form-urlencoded"; string reqString = "method=getQuote&key=457653&format=xml&lang=en"; byte[] reqData = Encoding.UTF8.GetBytes(reqString); req.ContentLength = reqData.Length; using (Stream reqStream = req.GetRequestStream()) reqStream.Write(reqData, 0, reqData.Length); using (WebResponse res = req.GetResponse()) using (Stream resSteam = res.GetResponseStream()) using (StreamReader sr = new StreamReader(resSteam)) { string xmlData = sr.ReadToEnd(); txtXmlData.Text = xmlData; Read(xmlData); } } private void Read(string xmlData) { XDocument doc = XDocument.Parse(xmlData); string quote = doc.Element("quote").Attribute("quoteText").Value; string auth = doc.Element("quote").Attribute("quoteAuthor").Value; txtQuoteResult.Text = "QUOTE: " + quote + "\r\n" + "AUTHOR: " + auth; }
My program launches using an Unhandled exception of the type "Fix System.NullReferenceException" occurred while trying to set the string value quote . I looked at some similar posts and made various changes, but I can't seem to set two line values.
Rawns source share