views:

1430

answers:

2

Does anyone know of a simple way of getting the raw xml that is returned from querying a webservice?

I have seen a way of doing this via Web Services Enhancements, but I don't want an added dependency.

+1  A: 

You have two real options. You can create a SoapExtension that will insert into the response stream and retrieve the raw XML or you can alter your proxy stubs to use XmlElement to retrieve the raw values for access in code.

For SoapExtension you want to be looking here: http://www.theserverside.net/tt/articles/showarticle.tss?id=SOAPExtensions

For XmlElement you want to look here: http://www.tech-archive.net/Archive/DotNet/microsoft.public.dotnet.framework.webservices/2006-09/msg00028.html

Wolfwyrd
A: 

So, here's the way I ended up doing it. The scenario is that a user clicks a button and wants to see the raw XML that a webservice is returning. This will give you that. I ended up using an xslt to remove the namespaces that get generated. If you don't, you end up with a bunch of annoying namespaces attributes in the XML.

        // Calling the webservice
        com.fake.exampleWebservice bs = new com.fake.exampleWebservice();
        string[] foo = bs.DummyMethod();

        // Serializing the returned object
        System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(foo.GetType());
        System.IO.MemoryStream ms = new System.IO.MemoryStream();
        x.Serialize(ms, foo);
        ms.Position = 0;

        // Getting rid of the annoying namespaces - optional
        System.Xml.XPath.XPathDocument doc = new System.Xml.XPath.XPathDocument(ms);
        System.Xml.Xsl.XslCompiledTransform xct = new System.Xml.Xsl.XslCompiledTransform();
        xct.Load(Server.MapPath("RemoveNamespace.xslt"));
        ms = new System.IO.MemoryStream();
        xct.Transform(doc, null, ms);

        // Outputting to client
        byte[] byteArray = ms.ToArray();
        Response.Clear();
        Response.AddHeader("Content-Disposition", "attachment; filename=results.xml");
        Response.AddHeader("Content-Length", byteArray.Length.ToString());
        Response.ContentType = "text/xml";
        Response.BinaryWrite(byteArray);
        Response.End();
VanOrman