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>
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