0

When I visit the page with the browser it is xml but when I request the url it is json. Is there something in mvc 4 api that makes it only output json to requests through the program or can I get xml back. Note: this is happening in a desktop application and a webpage so it has to be a setting of some sort and here was the error that the desktop gave me: Data at the root level is invalid. Line 1, position 1. And yes I am loading the doc right and I checked the value by using json instead and it came back ok.

 private void Plus_Click(object sender, EventArgs e)
        {
            string FValue = id.Text;
            string SValue = id2.Text;
            string ending;
            string url = "http://localhost:56254/api/add?id=" + FValue + "&id2=" + SValue;
            XmlDocument xdoc = new XmlDocument();

            xdoc.Load(url);

            XmlNode xNode = xdoc.SelectSingleNode("End");
            ending = xNode.InnerText;
            Answer.Text = ending;
        }

This is my desktop application code. My code it gets the xml is right here:

namespace Calculator.Controllers
{
    using Calculator.Models;
    public class AddController : ApiController
    {

        public Calcs GetAddition(int id, int id2)
        {
           double end = id + id2;
            Calcs[] calcs = new Calcs[] { new Calcs { FValue = id, SValue = id2, End = end } };
            return calcs[0];
        }
    }
}

Here is Calcs:

namespace Calculator.Models

    {
        public class Calcs
        {
            public int FValue { get; set; }
            public int SValue { get; set; }
            public double End { get; set; }
        }
    }

Here is what the browser puts out:

<Calcs xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/Calculator.Models">
<End>60</End>
<FValue>55</FValue>
<SValue>5</SValue>
</Calcs>
Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291
wizage
  • 304
  • 4
  • 20

1 Answers1

2

You need to set the accept header type in your request. However, since you neglected to provide enough information to know how you were requesting the data, that's as far as I can help you.\

EDIT:

The problem here is that XmlDocument.Load does not send an accept header that includes xml in the accepted formats (kind of stupid really, one would think It would).

You probably have to use an WebRequest to retrieve the document, specifying an accept header indicating "application/xml;q=0.9".

You can find a solution here: C# Won't Load A Certain XML, But Works in Browser

Community
  • 1
  • 1
Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291
  • @wizage - that is the easiest way. It's a few lines of code. Your only other solution is to rewrite your action method to return XML explicitly, either by installing the MVC Contrib for an XmlResult type or formatting the XML manually. – Erik Funkenbusch Jun 20 '12 at 21:36
  • @wizage - I just noticed you're using WebAPI, which wasn't clear from your post, and your tagging is messed up. WebAPI is designed to return the type you request via the accept header. That's what it's for. There is no "easier way", it's just a couple lines of code. – Erik Funkenbusch Jun 20 '12 at 21:38
  • Dang that sucks... why would they code that so horribly. Thanks! – wizage Jun 20 '12 at 21:39
  • @wizage - they didn't. It's designed to allow you to get data in whatever format you want. The requester asks for the format. Read this: http://weblogs.asp.net/gunnarpeipman/archive/2012/04/19/asp-net-web-api-how-content-negotiation-works.aspx – Erik Funkenbusch Jun 20 '12 at 21:41
  • If you are using .NET 4 you should use the new HttpClient rather than WebRequest. – starskythehutch Jun 20 '12 at 21:43
  • @MystereMan It works and thanks I now understand thanks for helping me get and answer and teaching me why lol. – wizage Jun 20 '12 at 21:46