Goodreads Developers discussion

583 views
.Net (Microsoft) GoodReads API example

Comments (showing 1-6 of 6) (6 new)    post a comment »
dateDown arrow    newest »

message 1: by Afeastman (new)

Afeastman | 3 comments Using the Goodreads API in .Net is a bit verbose but not difficult. This sample method creates a new XML subset from the GoodReads stream. I choose to use XPath but XSLT is another valid option for the XML manipulation.

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.:]


message 2: by Michael (new)

Michael Economy (michaeleconomy) Thanks for sharing!


message 3: by Steve (new)

Steve H. (shinmn) | 8 comments Afeastman wrote: "Using the Goodreads API in .Net is a bit verbose but not difficult. This sample method creates a new XML subset from the GoodReads stream. I choose to use XPath but XSLT is another valid option f..."

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


message 4: by Afeastman (new)

Afeastman | 3 comments Shinmn,

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!


message 5: by Lindsey (new)

Lindsey B (LindseyBieda) | 3 comments Building upon this I wrote some tutorials on using OAuth in C# using goodreads as my example.

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.


message 6: by Michael (new)

Michael Economy (michaeleconomy) Awesome!


back to top

unread topics | mark unread