1

Using a Razor form (Html.BeginForm()) I am calling an action in my BooksController which making an api call to www.isbndb.com to get book information. After serializing the XML response, I am unsure how to pass that data back to use in the calling view.

Create.cshtml

@using (Html.BeginForm("searchForISBN", "Books"))
        {
            @Html.TextBoxFor(m => m.Title)
            <input class="btn " id="searchForISBN" type="submit" value="Search" />
        }

XML response

<?xml version="1.0" encoding="UTF-8"?>
    <ISBNdb server_time="2005-07-29T03:02:22">
        <BookList total_results="1">
            <BookData book_id="paul_laurence_dunbar" isbn="0766013502">
                <Title>Paul Laurence Dunbar</Title>
                <TitleLong>Paul Laurence Dunbar: portrait of a poet</TitleLong>
                <AuthorsText>Catherine Reef</AuthorsText>
                <PublisherText publisher_id="enslow_publishers">
                    Berkeley Heights, NJ: Enslow Publishers, c2000.</PublisherText>
                <Summary> A biography of....</Summary>
                <Notes>"Works by Paul Laurence Dunbar": p. 113-114.
                   Includes bibliographical references (p. 124) and index.</Notes>
                <UrlsText></UrlsText>
                <AwardsText></AwardsText>
             </BookData>
        </BookList>
  </ISBNdb>

BooksController Action

public StringBuilder searchForISBN(Books books)
    {
        HttpWebRequest request = WebRequest.Create("http://isbndb.com/api/books.xml?access_key=xxxxxx&index1=title&value1="+books.Title) as HttpWebRequest;
        string result = null;
        using (HttpWebResponse resp = request.GetResponse() as HttpWebResponse)
        {
            StreamReader reader = new StreamReader(resp.GetResponseStream());
            result = reader.ReadToEnd();
        }
        StringBuilder output = new StringBuilder();
        using (XmlReader reader = XmlReader.Create(new StringReader(result)))
        {
            reader.ReadToFollowing("BookList");
            reader.MoveToFirstAttribute();
            reader.MoveToNextAttribute();
            reader.MoveToNextAttribute();
            reader.MoveToNextAttribute();
            string shown_results = reader.Value;
            int numResults;
            bool parsed = Int32.TryParse(shown_results, out numResults);

            for (int i = 0; i < numResults; i++)
            {
                reader.ReadToFollowing("BookData");
                reader.MoveToFirstAttribute();
                reader.MoveToNextAttribute();
                string isbn = reader.Value;
                output.AppendLine("The isbn value: " + isbn);
            }
        }
        return ouput;
    }

I am trying to return the data to the calling view to display the results below the search-bar form in Create.cshtml. Currently it is returning a new view with the path localhost:xxxxx/Books/searchForISBN.

Any ideas on how to do this?

2 Answers 2

1

XmlReader is a low-level API that's meant for performance-critical code, and, as you've surmised, it doesn't, by itself, create an object for you that can be passed to the view.

You might want to consider creating your own strongly-typed object that encapsulates all of the information returned by isbndb.com. You could also, in a pinch, use the XmlDocument class, and query properties off of it using XPath.

    XmlDocument doc = new XmlDocument();
    doc.Load(stream);
    string xpath = "data/element[@id='paul_laurence_dunbar']/AuthorsText";
    XmlNode node = doc.SelectSingleNode(xpath);
    if (node != null) {
          Console.WriteLine(node.InnerText);
    }   

HTH. Clay

Sign up to request clarification or add additional context in comments.

Comments

0

You could return the View directly. Like such:

return View("CallingViewName",model);

You may have to build a model of the serialized XML book info that you'd want to pass back to the view though.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.