3

I have an XML reader but I receive an error when I'm trying to read the XML from an URL (external source).

This is the code I have ATM:

XmlReader xmlReader = XmlReader.Create("http://dl.bukkit.org/api/1.0/downloads/projects/craftbukkit/view/build-1330/");
        while (xmlReader.Read())
        {

        }

Very simple code, but it returns an error which says:

Data at the root level is invalid. Line 1, position 1.

Any idea?
I can't edit the XML, because it's not mine.

Thanks in advance!

4
  • What does the Xml doc look like if you try to access it from your web browser? Commented Apr 11, 2012 at 8:06
  • 4
    It's not Xml, it's Json. Commented Apr 11, 2012 at 8:06
  • @Phil This might be a noob question, but is it hard to read Json in C#? I'm still a little bit new... EDIT: Because when I read the file via WebClient I get an unformatted file back. Commented Apr 11, 2012 at 8:11
  • I don't know much about Json deserializers. I'm sure someone will help. Commented Apr 11, 2012 at 8:19

1 Answer 1

4

If you use Fiddler to analyze the response returned by the sever, you'll see, that you get JSON instead of XML. You can add a parameter to the URL to get XML:

http://dl.bukkit.org/api/1.0/downloads/projects/craftbukkit/view/build-1330/?format=xml
Sign up to request clarification or add additional context in comments.

3 Comments

So what this does is it converts the page to XML? It seems to work tho :).
You have three options here. First, you can set the Accept header to text/xml, which seems to be a little bit tricky with the XmlReader. Second, you can add the parameter which is a feature of the API you are using and therefore isn't standardized. Third, you can parse the JSON returned by the web service using the DataContractJsonSerializer or other third-party components (which would be the fastest approach of the three).
Json seems a little bit hard for me :(, I think im going with the second option, the parameter. But I'm going to look at the third option for sure, thanks!

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.