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;
    }
Amar
  • 509
  • 2
  • 15
  • 36
  • Please check answers here - https://stackoverflow.com/questions/26612835/convert-xml-to-json-array-when-only-one-object – Akash Sep 13 '18 at 21:58
  • What is the XML input and what is the output you are expecting? – MBB Sep 14 '18 at 06:54
  • XML input is my data source and is formatted like this: https://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. – Amar Sep 14 '18 at 08:07

1 Answers1

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);
            });
        }
    });
});
MBB
  • 1,635
  • 3
  • 9
  • 19