'Getting JSON instead of XML file when calling a service with WebClient
I have looked for answers everywhere, but nothing has answered my question thus far. For the application that I'm making, I need to obtain the latest XML artifact from Bukkit.org. Now, if I save the XML artifact manually (through my browser) and then load it into my program, it works properly, giving me the expected formatted XML file.
However, if I use WebClient to access the file, I run into issues, as the file Webclient downloads is not even in XML format. I have put a test case below.
static void manualLoad()
{
//local copy
XDocument doc = XDocument.Load("artifacts.xml");
var lol = doc.Descendants("build_number");
foreach (XElement e in lol)
{
Console.WriteLine(e.Value); //correct output
}
}
static void onlineLoad()
{
WebClient client = new WebClient();
//save to local project folder
client.DownloadFile(new Uri("http://dl.bukkit.org/api/1.0/downloads/projects/bukkit/artifacts/")
, "C:\\...\\XMLTest\\XMLTest\\bin\\Debug\\lol.xml");
XDocument doc = XDocument.Load("lol.xml"); //error thrown!
}
static void Main(string[] args)
{
manualLoad(); //works!
onlineLoad(); //throws XMLexception: Data at root level is invalid.
Console.ReadKey();
}
The XML artifact I am trying to access is here:Bukkit.org XML Artifact
Solution 1:[1]
I haven't tried your code, but it seems you have to set the accept header to application/xml - looks like it defaults to json.
Edit: You can set the header on the WebClient:
client.Headers.Add("accept", "application/xml");
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
