如何获取从web服务请求返回的原始xml?
问题描述:
有没有人知道一个简单的方法来获取从查询Web服务返回的原始XML?如何获取从web服务请求返回的原始xml?
我已经看到了通过Web Services Enhancements这样做的一种方法,但我不想添加依赖项。
答
你有两个真正的选择。您可以创建一个SoapExtension,将其插入到响应流中并检索原始XML,或者可以更改代理存根以使用XmlElement检索代码中访问的原始值。
有关的SoapExtension您想寻找这里:http://www.theserverside.net/tt/articles/showarticle.tss?id=SOAPExtensions
对于XmlElement的你想在这里看看:http://www.tech-archive.net/Archive/DotNet/microsoft.public.dotnet.framework.webservices/2006-09/msg00028.html
答
所以,这里是我最后做的方式。这种情况是,用户点击一个按钮,并希望看到Web服务正在返回的原始XML。这会给你。我最终使用xslt去除了生成的命名空间。如果你不这样做,你最终会在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();
我说得对不对的理解是你被反序列化对象和序列化回,即它不是由服务返回的原始XML数据? – 2011-02-09 08:29:59