- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在开发 Windows 应用商店应用程序。
我有以下手动创建的故障类:
[XmlRoot(Namespace = "http://schemas.xmlsoap.org/soap/envelope/", ElementName = "Fault")]
public partial class SoapFault
{
[XmlElement(Form = XmlSchemaForm.Unqualified, ElementName = "faultcode")]
public String FaultCode { get; set; }
[XmlElement(Form = XmlSchemaForm.Unqualified, ElementName = "faultstring")]
public String FaultDescription { get; set; }
[XmlElement(Form = XmlSchemaForm.Unqualified, ElementName = "detail")]
public InnerException[] Detail { get; set; }
}
[XmlType(Namespace = "http://my.namespace.com", TypeName = "InnerException")]
public partial class InnerException
{
[XmlElement(Form = XmlSchemaForm.Unqualified, ElementName = "message")]
public String Message { get; set; }
}
这是我正在尝试读取的服务器的回复:
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"><env:Body><env:Fault><faultcode>env:Server</faultcode><faultstring>internal error</faultstring><detail><ns2:InnerException xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://ns1.my.namespace.com" xmlns:ns2="http://my.namespace.com" xmlns:ns3="http://ns3.my.namespace.com"><message>internal error</message></ns2:InnerException ></detail></env:Fault></env:Body></env:Envelope>
我尝试阅读它的方式如下:
using (XmlReader reader = XmlReader.Create(await response.Content.ReadAsStreamAsync()))
{
string SoapNamespace = "http://schemas.xmlsoap.org/soap/envelope/";
try
{
var serializer = new XmlSerializer(typeof(SoapFault));
reader.ReadStartElement("Envelope", SoapNamespace);
reader.ReadStartElement("Body", SoapNamespace);
var fault = serializer.Deserialize(reader) as SoapFault;
reader.ReadEndElement();
reader.ReadEndElement();
}
catch(Exception ex)
{
throw new Exception("Exception was thrown:" + ex.Message);
}
}
我已经尝试添加 namespace 、更改 XmlElement 属性,但我总是以 SoapFault 中的 Detail 属性为 NULL 告终。当我将类型更改为对象时,我至少得到一个包含数据的 XmlNode 集。
我必须在此代码中更改什么才能在序列化时实例化正确的类?
请注意:不幸的是,我不得不手动创建调用,无法使用自动生成的代码。
最佳答案
深受 How to Deserialize XML document 启发,我认为这应该可以解决问题。
我生成的类如下:
这导致:
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.269
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
using System.Xml.Serialization;
//
// This source code was auto-generated by xsd, Version=4.0.30319.1.
//
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://schemas.xmlsoap.org/soap/envelope/")]
[System.Xml.Serialization.XmlRootAttribute(Namespace="http://schemas.xmlsoap.org/soap/envelope/", IsNullable=false)]
public partial class Envelope {
private EnvelopeBody[] itemsField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("Body")]
public EnvelopeBody[] Items {
get {
return this.itemsField;
}
set {
this.itemsField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://schemas.xmlsoap.org/soap/envelope/")]
public partial class EnvelopeBody {
private EnvelopeBodyFault[] faultField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("Fault")]
public EnvelopeBodyFault[] Fault {
get {
return this.faultField;
}
set {
this.faultField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://schemas.xmlsoap.org/soap/envelope/")]
public partial class EnvelopeBodyFault {
private string faultcodeField;
private string faultstringField;
private EnvelopeBodyFaultDetail detailField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string faultcode {
get {
return this.faultcodeField;
}
set {
this.faultcodeField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string faultstring {
get {
return this.faultstringField;
}
set {
this.faultstringField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public EnvelopeBodyFaultDetail detail {
get {
return this.detailField;
}
set {
this.detailField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://schemas.xmlsoap.org/soap/envelope/")]
public partial class EnvelopeBodyFaultDetail {
private InnerException innerExceptionField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Namespace="http://my.namespace.com")]
public InnerException InnerException {
get {
return this.innerExceptionField;
}
set {
this.innerExceptionField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://my.namespace.com")]
[System.Xml.Serialization.XmlRootAttribute(Namespace="http://my.namespace.com", IsNullable=false)]
public partial class InnerException {
private string messageField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string message {
get {
return this.messageField;
}
set {
this.messageField = value;
}
}
}
是的,它是自动生成的,但我认为这是创建可以反序列化的类的最简单方法。你可以这样做:
XmlDocument document = new XmlDocument();
document.Load(Server.MapPath("~/Data/reply.xml"));
System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(Envelope));
Envelope envelope = (Envelope)serializer.Deserialize(new StringReader(document.OuterXml));
它正确地获取了 Detail
属性。在您的情况下,您需要像以前一样使用 XmlReader
而不是我的 new StringReader(document.OuterXml)
,并将其包装在 using block 中,等等。
请记住,反序列化对象的某些属性名称与您在 SoapFault
类中的名称不完全匹配(它现在称为 Envelope
,例如),并且一些属性表示为数组而不是单个对象,但如果需要,您可以调整 Xsd 以适应。
关于c# - 反序列化 SOAP 消息的详细信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12855488/
我正在使用 SOA 客户端 Firefox 插件向某些 ONVIF 摄像机发送 SOAP 请求。您将在下面看到“GetServices”请求。它对于一台相机工作正常,但对于另一台(AXIS 相机)我收
我正在使用 SOA 客户端 Firefox 插件向某些 ONVIF 摄像机发送 SOAP 请求。您将在下面看到“GetServices”请求。它对于一台相机工作正常,但对于另一台(AXIS 相机)我收
有谁知道 Fiddler 是否可以显示 ASMX Web 服务的原始 SOAP 消息?我正在使用 Fiddler2 和 Storm 测试一个简单的 Web 服务,结果各不相同(Fiddler 显示纯
使用 SOAP 协议(protocol)时,是否可以使用 SOAP 取消挂起的远程函数调用? 我看到三种不同的情况: A) 向需要很长时间才能完成的服务发出请求。例如,当复制包含大量文件的目录时,可以
人们还在写吗SOAP services还是已经通过了它的技术architectural shelf life ?人们是否回归二进制格式? 最佳答案 SOAP 的替代方案不是二进制格式。 我认为您看到了
SOAP 协议(protocol)工作的默认端口号是多少? 最佳答案 没有“SOAP 协议(protocol)”之类的东西。 SOAP 是一种 XML 模式。 但是,它通常通过 HTTP(端口 80)
之间有什么区别 和 以及如何在它们之间切换? 如何将响应从 具有定义的命名空间 "http://schemas.xmlsoap.org/soap/envelope/" ,它的特殊含义是底层 XML
我正在从 Mule 进行 SOAP 调用。我正在使用 default-exception-strategy 来捕获异常。发生异常时,如何发送我自己的故障代码和故障字符串而不是通用的 soap 故障消息
我正在编写一个 powershell 脚本,它将每 10 分钟 ping 一次soap web 服务,以使其保持活跃状态,从而提高性能。我们已经在 IIS 中尝试了多种技术,应用程序池空闲超时和只
如有任何帮助,我们将不胜感激;我已经研究了几天了。 下面是我目前得到的代码;不幸的是,当我运行它时出现 HTTP 415 错误; 无法处理消息,因为内容类型为“text/xml; charset=UT
我们需要使用其他团队开发的网络服务。使用 JAX-WS用于生成网络服务。我们正在使用 wsimport 生成客户端 stub 。 问题是我需要将以下信息作为 header 与 SOAP 正文一起传递:
我的意思是,真正的互操作:从 Java 到 .NET,从 PHP 到 Java,等等。 我之所以这样问,是因为我们的权力希望我们使用 SOAP Web 服务实现面向公众的 API,并且我试图强调支持
我写了一个拦截器进行测试。但是我在Interceptor中获得的Soap消息正文始终为null。 我的Cxf是Apache-CXF-2.4.0 bean.xml是这样的:
我正在尝试查询货币的 netsuite api。以下soap请求在SOAP UI客户端中对我有用。但是我很难尝试使用 ruby 的 savon gem 0.9.7 版进行相同的工作。
我创建了一个示例 Mule 流,首先根据 http://www.mulesoft.org/documentation/display/current/Consuming+Web+Services+wi
我正在尝试使用此 SOAP 服务:http://testws.truckstop.com:8080/v13/Posting/LoadPosting.svc?singleWsdl使用 node-soap
我有几个 SoapUI 测试步骤,其中响应返回空(即“-> 空/空响应”),这正是我所期望的。 如何断言对测试步骤请求的响应为空? 到目前为止,我已经尝试了以下但没有运气: 审查可用的断言,无需定制
我正在尝试构建一个手动 HTTP 请求,以便从我认为是相当简单的 SOAP Web 服务调用中返回响应。但是,我无法正确构建请求,并且没有得到我期望的响应。 适用wsdl声明: wsdl 目标命名空间
我正在尝试使用 Insomnia 调用 SOAP 电话 - 特别是试图让帖子成功。我将 URL 定义为端点,并将正文类型作为带有 SOAP 内容(信封、标题、正文)的 XML。我在标题中定义了用户 I
我正在学习 SOAP 实现,并且对于 SOAP 1.2 信封的适当 namespace URI 感到有些困惑。 w3c specification for SOAP指的是“http://www.w3.
我是一名优秀的程序员,十分优秀!