Goodreads Developers discussion
.Net (Microsoft) GoodReads API example
date
newest »


Did you consider using XDocument (System.Xml.Linq)?

I did but I wanted to use some Regular Expressions in my process and decided to stay with basic C# code. You are correct that Linq is a valid way to handle the XML document received from GoodReads. Sometimes I think we have too many options!

Part I: http://rarlindseysmash.com/index.php?...
Part II: http://rarlindseysmash.com/index.php?...
Hopefully this helps some other C# hackers out there looking to dive into the goodreads API.
public XmlDocument GetBookShelf()
{
string ShelfUrl =
"http://www.goodreads.com/review/list/... Member ID:].xml?key=[Your Key:]&v=2&shelf=to-read&per_page=64";
HttpWebRequest webRequest =
(HttpWebRequest)(System.Net.WebRequest.Create(ShelfUrl));
// Set up a Web Request
webRequest.Accept = "text/xml";
webRequest.Credentials = System.Net.CredentialCache.DefaultCredentials;
webRequest.Method = WebRequestMethods.Http.Get;
// Set up a Web Response
HttpWebResponse xwebResponse = (HttpWebResponse)webRequest.GetResponse();
// Let's stream the XML to an XPath document
Stream responseStream = xwebResponse.GetResponseStream();
XmlTextReader reader = new XmlTextReader(responseStream);
// We are finished with GoodReads - the XML formated data is now in our document.
XPathDocument doc = new XPathDocument(reader);
// Close the stream and close the web response.
reader.Close();
xwebResponse.Close();
// We need a Navigator for out XPath work
XPathNavigator myXPathNavigator = doc.CreateNavigator();
// Move to the 'reviews' node
XPathExpression query = myXPathNavigator.Compile("//reviews");
XPathNavigator node = myXPathNavigator.SelectSingleNode(query);
bool rtrnCde = false;
int intNodeValue = 0;
// Move to the 'end' attrib to get the book count and use it to set up string arrays.
rtrnCde = node.MoveToFirstAttribute();
rtrnCde = node.MoveToNextAttribute();
if (rtrnCde)
{
string strAttrName = node.Name.ToString();
if (strAttrName == "end")
intNodeValue = node.ValueAsInt;
else
intNodeValue = 1;
}
// There are a number of ways to query the XML using XPath. This is one easy way.
string[:] Books;
Books = new string[intNodeValue:];
XPathNodeIterator nodesBook = myXPathNavigator.SelectDescendants("book", "", false);
int cnt = 0;
while (nodesBook.MoveNext())
{
Books[cnt:] = nodesBook.Current.InnerXml;
cnt++;
}
[add your own code here to finish whatever you are trying to do - the complete function returns an xmldocument to a WPF XAML script in my code.:]