无法反序列化消息

时间:2013-12-23 23:38:59

标签: xml soap nested deserialization

我在反序列化以下XML时遇到了一些麻烦。我可以让订单填写得很好,但是嵌套的软件包给我带来了一些艰难的时刻。在序列化之前清除作为soap消息的消息。我认为问题在于包标签中的XSI属性?

请注意:以下XML来自SOAP消息,它是从getSoapBody(string)函数返回的。所有其他(40左右)肥皂反应都很好,它只是我遇到问题的双嵌套后缀。

<?xml version="1.0" encoding="utf-8"?>
<Orders>
  <Order>
    <OrderNumber xsi:type="xsd:int">12345</OrderNumber>
    <Promotion xsi:type="xsd:string" />
    <Agent_Contact xsi:type="xsd:string" />
    <Packages>
        <Package>
          <OrderPackageType_ID xsi:type="xsd:int">1</OrderPackageType_ID>
          <Title_ID xsi:type="xsd:int">1</Title_ID>
          <StartIssue xsi:type="xsd:string">November 2010</StartIssue>
          <EndIssue xsi:type="xsd:string" />
        </Package>
        <Package>
          <OrderPackageType_ID xsi:type="xsd:int">2</OrderPackageType_ID>
          <Title_ID xsi:type="xsd:int">1</Title_ID>
          <StartIssue xsi:type="xsd:string">October 2010</StartIssue>
          <EndIssue xsi:type="xsd:string" />
         </Package>
      </Packages>      
    </Order>
    <Order> .... </Order>
</Orders>

去酯化的代码如下

        NameSpaceManager.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance")

        'Initialize our parser context for the xsi namespace
        Context = New XmlParserContext(Nothing, NameSpaceManager, Nothing, XmlSpace.None)

        'Set up our reader settings
        ReaderSettings = New XmlReaderSettings()
        ReaderSettings.ConformanceLevel = ConformanceLevel.Fragment

        'getSoapBody(Response) rips out the env stuff and sets up for serialization this works
        'fine and I can populate all other SOAP messages fine. It just returns elements inside 
        'the SOAP response body
        Reader = XmlReader.Create(New StringReader(GetSoapBody(Response)), ReaderSettings, Context)

        If Deserializer.CanDeserialize(Reader) Then
            'Deserialize here
            ResponseObject = CType(Deserializer.Deserialize(Reader), U)
        Else
            Throw New SerializationException("Unable to deserialize class object")
        End If

订单填充到我的订单类,包没有返回什么?
我在订单类中使用了包列表。

''' <remarks/>
<Serializable()> _
<XmlType("Order", AnonymousType:=True, NameSpace:="http://www.w3.org/1999/XMLSchema-instance")> _
Public Class Order
     'other memebers first
       .....
      'nested packed class declaration
       <XmlElement("Package", NameSpace:="http://www.w3.org/1999/XMLSchema-instance")> _
       Public Property Packages() As List(Of Package)
          Get
            If _Packages Is Nothing Then
               _Packages = New List(Of Package)
            End If
            Return _Packages
           End Get
           Set(ByVal value As List(Of Package))
             _Packages = value
           End Set
       End Property
End Class

我的包类按以下方式进行修饰。

<Serializable()> _
<XmlType("Package", AnonymousType:=True, NameSpace:="http://www.w3.org/1999/XMLSchema-instance"> _
 Public Class Package 
         <XmlElementAttribute("OrderPackageType_ID", Form:=XmlSchemaForm.Unqualified, IsNullable:=True)> _
        Public Property OrderPackageType_ID() As NullableInt
          Get
            Return _OrderPackageType_ID
          End Get
          Set(ByVal value As NullableInt)
            _OrderPackageType_ID = value
          End Set
       End Property
        ....
 End Class

使用以下XML填充packages类。但作为嵌套元素,如上所述,它返回空?

<?xml version="1.0" encoding="utf-8"?>
      <Packages>
        <Package>
          <OrderPackageType_ID xsi:type="xsd:int">1</OrderPackageType_ID>
          <Title_ID xsi:type="xsd:int">1</Title_ID>
          <StartIssue xsi:type="xsd:string">November 2010</StartIssue>
          <EndIssue xsi:type="xsd:string" />
        </Package>
        <Package>
          <OrderPackageType_ID xsi:type="xsd:int">2</OrderPackageType_ID>
          <Title_ID xsi:type="xsd:int">1</Title_ID>
          <StartIssue xsi:type="xsd:string">October 2010</StartIssue>
          <EndIssue xsi:type="xsd:string" />
         </Package>
     </Packages>      

我们将不胜感激,希望我能为您提供足够的信息。

问候......

1 个答案:

答案 0 :(得分:0)

我以为我最好回答这个问题,我刚才找到了答案。我需要正确地装饰属性和类,类似于以下内容。这解决了我的问题。

    <System.Xml.Serialization.XmlArrayAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)> _
<System.Xml.Serialization.XmlArrayItemAttribute("Package", GetType(Package), Form:=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable:=True)> _
    Public Property Packages() As Package()
        Get
            Return _Packages
        End Get
        Set(ByVal value As Package())
            _Packages = value
        End Set
    End Property
End Class


<System.Xml.Serialization.XmlRoot("Package")> _
Public Class Package
....
End Class