XmlSerializer - 反序列化 SoapException 的详细信息元素

3

我正在尝试反序列化从 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;
        }
    }
}

谢谢!


3
它是一个部分类这个事实与此无关,顺带一提。 - Marc Gravell
2个回答

4
在RootElement上设置命名空间就可以解决问题了。
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;

1
老兄!在我费尽三个小时的口舌之争后,这真是救了我的一命... - Craig Eddy

1
首先,除非你没有其他选择,否则不要使用new XmlTextReader()。自.NET 2.0以来已被弃用。
其次,您已经有XML了-为什么要将其转换为字符串,然后再转换回XML?尝试使用
XmlReader xr = soapex.Detail.FirstChild.CreateNavigator().ReadSubTree();
Fault fault = xs.Deserialize(xr) as Fault;    

我收到了相同的异常:“<fault xmlns='http://www.usps.com/postalone/services/mailxml80/MailXML80ALLMsgType'> was not expected." - Michael G
@Michael:这不是你发布的相同XML。请注意,最后一个是usps.com。 - John Saunders

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接