C# 如何获取从 Web 服务请求返回的原始 xml?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/314696/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 22:49:49  来源:igfitidea点击:

How to get the raw xml returned from a webservice request?

c#xmlweb-services

提问by VanOrman

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

有谁知道获取从查询 Web 服务返回的原始 xml 的简单方法?

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

我已经看到了通过Web Services Enhancements执行此操作的方法,但我不希望增加依赖项。

回答by Wolfwyrd

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.

你有两个真正的选择。您可以创建一个将插入响应流并检索原始 XML 的 SoapExtension,或者您可以更改您的代理存根以使用 XmlElement 检索原始值以在代码中访问。

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

对于 SoapExtension,您想在这里查看: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

对于 XmlElement,您想在这里查看:http: //www.tech-archive.net/Archive/DotNet/microsoft.public.dotnet.framework.webservices/2006-09/msg00028.html

回答by VanOrman

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.

所以,这就是我最终这样做的方式。该场景是用户单击按钮并希望查看 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();