tags:

views:

407

answers:

3

I'm trying to load an xml from a url in C#, but the problem is that, there is an xsl attached to the xml file, which means that I don't get the content of the xml file, but the html that it's transformed to using the xsl.

Is there any way to load the xml without first transforming it, so I just get the content of the xml?

A: 

If the server transforms the file prior to serving it, then no, you can only get the transformed version. However, if it only specifies a processing instruction that refers an XSL stylesheet and the browser transforms it, you get the XML by default.

Mehrdad Afshari
The xsl is specified in the xml like this: <?xml-stylesheet type="text/xsl" href="/layout/style/sheet.xsl"?>Shouldn't this mean that I'll get the xml and not the transformed version? If that's the case shouldn't I be able to load it using XmlDocument.Load()?
Tuvix
Of course you'll get the XML. `XmlDocument` will not automatically transform anything.
Mehrdad Afshari
A: 

Have you tried setting the Accept property of your HttpWebRequest object to "text/xml"?

CptSkippy
Would this property make a difference in getting a transformed XML vs original XML?
Charles Prakash Dasari
The Accept Property represents the value of the Accept Header in an HTTP Request. It defines what MIME-Types you accept/want and the preference given that multiple choices are available. By default the request object prefers text/HTML, and so the responding web server might be accommodating that preference. I was suggesting setting the preference to XML on the off chance it might change the server's response.
CptSkippy
I get the same result if I use the HttpWebRequest object and set it's accept property to text/xml.
Tuvix
A: 

The fact that the URL has ".xml" in it doesn't mean that the server is going to give you XML. In the case of the URL you provided in your example, the server's emitting XHTML. (I also am not seeing the xml-stylesheet processing directive that you mention in your comment in that file, which makes me wonder if you're looking at two different things.) There's nothing you can do on the client side to change that. If there's a way of formulating the URL to get raw XML from the server, it's not documented on that site.

They also seem to be serving up XHTML that isn't well-formed XML (judging on what happens when you try to parse it). That's not very nice of them.

Edit:

Okay, so the culprit here is that this site apparently checks the user agent to determine whether it should send XML or HTML. The answer here (any why, pray, isn't Martin Honnen on StackOverflow?) shows how to do it:

    string url = "http://eu.wowarmory.com/character-sheet.xml?r=Stormreaver&amp;n=Sebassis";

    HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(url);
    httpRequest.UserAgent = @"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)";

    using (HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse())
    {
        XmlDocument xmlDocument = new XmlDocument();
        xmlDocument.Load(httpResponse.GetResponseStream());
        Console.Write(xmlDocument.OuterXml);
    }

I wish I could tell you why they're doing that, and why this user agent works while others don't, but it's a little outside my area of expertise. Maybe someone else can shed some light on this.

Robert Rossney
I've tried opening the XML in Notepad++. When I do this I get the actual XML and not the transformed result. This is also where I saw the xml-stylesheet processing directive. That's why I don't understand why I can't open the XML from .Net.
Tuvix
That did it! Thank you very much for the help.
Tuvix