gpt4 book ai didi

c# - XmlSerializer - 反序列化 SoapException 详细元素

转载 作者:行者123 更新时间:2023-11-30 21:17:48 25 4
gpt4 key购买 nike

我正在尝试反序列化从 Web 服务接收到的 SoapException 消息。我收到以下异常:

System.InvalidOperationException was unhandled
Message="There is an error in XML document (2, 2)."
Source="System.Xml"
StackTrace:
at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader)
at FullServiceServices.AddressCorrectionService.GetResponse(FullServiceAddressCorrectionQueryRequest request) in AddressCorrectionService.cs:line 169
at FullServiceServices.AddressCorrectionService.GenerateRequest(DateTime startDate, DateTime endDate) in AddressCorrectionService.cs:line 79
at FullServiceServices.Form1.Form1_Load(Object sender, EventArgs e) in \Form1.cs:line 33

InnerException: System.InvalidOperationException
Message="<Fault xmlns='http://idealliance.org/maildat/Specs/md091/mailxml81/mailxml'> was not expected."
Source="gn4ggxxc"
StackTrace:
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderFault.Read3_Fault()

如何将此 XML 正确反序列化为提供的分部类 Fault?


下面是我如何将 XML 字符串读入 XMLSerializer:

            XmlTextReader xr = new XmlTextReader(new StringReader(soapEx.Detail.FirstChild.InnerText));
XmlSerializer xs = new XmlSerializer(typeof(Fault));
Fault fault = xs.Deserialize(xr) as Fault;

这是 soap 异常 .Detail 属性的 FirstChild 的 InnerText

<?xml version="1.0" encoding="UTF-8"?>
<mailxml:Fault xmlns:mailxml="http://idealliance.org/maildat/Specs/md091/mailxml81/mailxml" xmlns:mailxml_base="http://idealliance.org/maildat/Specs/md091/mailxml81/base" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://idealliance.org/maildat/Specs/md091/mailxml81/mailxml mailxml_031910.xsd http://idealliance.org/maildat/Specs/md091/mailxml81/base mailxml_base_031910.xsd">
<mailxml:FaultCode>402</mailxml:FaultCode>
<mailxml:FaultDescription>402 Not Well Formed XML</mailxml:FaultDescription>
</mailxml:Fault>

这是通过 WSDL 自动生成的部分类。

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "2.0.50727.3082")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://idealliance.org/maildat/Specs/md091/mailxml81/mailxml")]
public partial class Fault {

private string[] faultCodeField;

private string[] faultDescriptionField;

private string trackingIDField;

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("FaultCode")]
public string[] FaultCode {
get {
return this.faultCodeField;
}
set {
this.faultCodeField = value;
}
}

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("FaultDescription")]
public string[] FaultDescription {
get {
return this.faultDescriptionField;
}
set {
this.faultDescriptionField = value;
}
}

/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute(Form=System.Xml.Schema.XmlSchemaForm.Qualified)]
public string TrackingID {
get {
return this.trackingIDField;
}
set {
this.trackingIDField = value;
}
}
}

谢谢!

最佳答案

在 RootElement 上设置 namespace 就可以了

XmlRootAttribute xRoot = new XmlRootAttribute();
xRoot.Namespace = "http://namespacehere";

XmlReader xr = soapEx.Detail.FirstChild.CreateNavigator().ReadSubtree();
XmlSerializer xs = new XmlSerializer(typeof(Fault), xRoot);

Fault fault = xs.Deserialize(xr) as Fault;

关于c# - XmlSerializer - 反序列化 SoapException 详细元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4597368/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com