0

I'm trying to create a web API. The problem I'm having is that I'm responding with a string instead of a better formatted response so the application using the API can parse it easier.

Is there a way to return something better formatted, like JSON or XML? And How do I do that?

  public string Get(string id)
    {
        XElement xelement = XElement.Load("C:/Users/potter/source/repos/library/lib/Controllers/books.xml");
        var books = xelement.Elements("book").Where(x => x.Element("title").ToString().ToLower().Contains(id));
        foreach (XElement xEle in books)
        {          
            returnValue = returnValue + xEle.ToString();    
        }
        return returnValue;
    }
3
  • Please check answers here - stackoverflow.com/questions/26612835/… Commented Sep 13, 2018 at 21:58
  • What is the XML input and what is the output you are expecting? Commented Sep 14, 2018 at 6:54
  • XML input is my data source and is formatted like this: msdn.microsoft.com/en-us/library/ms256479(v=vs.110).aspx Now I'm trying to respond a specific book to the application as a formatted format instead of a string with bunch of words. Commented Sep 14, 2018 at 8:07

1 Answer 1

1

Hi Couple of points -

  1. Is there a way to return something better formatted, like JSON or XML? And How do I do that?

From WEB API perspective if the object is serializable then it will automatically convert to well formatted XML or JSON. So instead of returning a string return the "books" which is an IEnumerable in this case so you don't have to worry about formatting.

You may have to also see these below discussions on the same --

WebAPI to Return XML

  1. You are trying to fetch specific books based on title

Would also want to suggest instead of checking against Value instead of XmlElement, So replace the below line

x => x.Element("title").ToString().ToLower().Contains(id)

with -

x => x.Element("title").Value.ToString().ToLower().Contains(id)

So the final code which you would be writing is for list of specific books -

 public IEnumerable<XElement> Get(string id)
        {

            XElement xelement = XElement.Load("C:/Users/potter/source/repos/library/lib/Controllers/books.xml");
            return xelement.Elements("book").Where(x => x.Element("title").Value.ToString().ToLower().Contains(id));
        }

Once you have this set up in server side then you can call the API from client side using jQuery ajax something like below -

 $(document).ready(function () {
    $.ajax({
        type: "GET",
        url: "http://localhost:50116/api/values/GetData/",//Change this to your URL
        cache: false,
        data: {id:'xml'},//Title has this word 'xml'
        dataType: "xml",
        success: function (xml) {
            $(xml).find('book').each(function () {
                var name = $(this).find("title").text();
                var genre = $(this).find("genre").text();
                var price = $(this).find("price").text();
                var description = $(this).find("description").text();
                alert(name);
                alert(genre);
                alert(price);
                alert(description);
            });
        }
    });
});
Sign up to request clarification or add additional context in comments.

4 Comments

This is excatly waht I'm looking for! Also Could I ask what the code on the View side would look like to get a specific element, lets say author? data["author"] ?
From view side how are you planning to call the API? Jquery Ajax?
Jquery with ajax indeed
Updated my answer please have a look!

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.