DataContractSerializer ReadObject(stream)返回空值

时间:2012-11-16 06:57:35

标签: wcf .net-4.0 deserialization

我无法将某些XML反序列化为适当的对象结构。当我在ReadObject(stream)上调用DataContractSerializer时,它会返回一个空数组。没有错误,不是空,只是一个空数组。

故事

我正在使用第三方服务,他们提供了一个wsdl。我已将wsdl作为服务引用添加到我的项目中。我已成功呼叫此服务并收到响应,该响应由.Net正确反序列化。所以我知道响应能够被反序列化。

我呼叫该服务一次以获取一些初始数据。然后,当第三方更新该数据时,他们将使用服务返回的相同响应XML POST到MVC URL。当我尝试从Request.InputStream反序列化XML时,我得到一个空数组。

我进行了搜索和搜索,但显然不是在正确的地方,因为我已经完全空白了。

我真的很感激你能提供的方向。

以下是正在使用的代码和文件:

我的反序列化代码 - 相当简单的东西,不是吗? updatedData是有问题的空数组。

    var dcs = new DataContractSerializer(typeof(IEnumerable<Account1>),
        "ListOfIrstAccountBrandAccountResponseIo",
        "http://www.siebel.com/xml/IRST%20Account%20Brand%20Account%20Response%20IO");

    var updatedData = (IEnumerable<Account1>)dcs.ReadObject(stream);

    return updatedData;

XML - 数据已经过清理,但对于此示例并不重要。

<ListOfIrstAccountBrandAccountResponseIo xmlns="http://www.siebel.com/xml/IRST%20Account%20Brand%20Account%20Response%20IO">
    <Account>
        <AccountStatus>Active</AccountStatus>
        <IRSTAccoutSectorNum>xxxxx</IRSTAccoutSectorNum>
        <MainFaxNumber>xxxxx</MainFaxNumber>
        <MainPhoneNumber>xxxxx</MainPhoneNumber>
        <Name>xxxxx</Name>
        <ParentAccountId></ParentAccountId>
        <ParentAccountName></ParentAccountName>
        <AccountRowId>xxxxx</AccountRowId>
        <ListOfCutAddress>
            <CutAddress>
                <City>xxxxx</City>
                <Country>xxxxx</Country>
                <County>xxxxx</County>
                <PostalCode>xxxxx</PostalCode>
                <PrimaryAddressFlg>x</PrimaryAddressFlg>
                <State>xx</State>
                <StreetAddress>xxxxx</StreetAddress>
                <StreetAddress2></StreetAddress2>
            </CutAddress>
        </ListOfCutAddress>
        <ListOfIrstBrandAccount>
            <IrstBrandAccount>
                <IRSTBrand>xxxxx</IRSTBrand>
                <IRSTBrandAccountNumber>xxxxx</IRSTBrandAccountNumber>
                <IRSTSIC>xx</IRSTSIC>
                <Name>xxxxx</Name>
            </IrstBrandAccount>
            <IrstBrandAccount>
                <IRSTBrand>xxxxx</IRSTBrand>
                <IRSTBrandAccountNumber>xxxxx</IRSTBrandAccountNumber>
                <IRSTSIC>xx</IRSTSIC>
                <Name>xxxxx</Name>
            </IrstBrandAccount>
            <IrstBrandAccount>
                <IRSTBrand>xxxxx</IRSTBrand>
                <IRSTBrandAccountNumber>xxxxx</IRSTBrandAccountNumber>
                <IRSTSIC>xx</IRSTSIC>
                <Name>xxxxx</Name>
            </IrstBrandAccount>
        </ListOfIrstBrandAccount>
        <ListOfInternalDivision>
            <InternalDivision>
                <Organization>xxxxx</Organization>
            </InternalDivision>
            <InternalDivision>
                <Organization>xxxxx</Organization>
            </InternalDivision>
        </ListOfInternalDivision>
        <ListOfServiceAgreement>
            <ServiceAgreement>
                <AccountId>xxxxx</AccountId>
                <AgreementId>xxxxx</AgreementId>
                <AgreementNumber>xxxxx</AgreementNumber>
                <AgreementStatus>xxxxx</AgreementStatus>
                <ListOfFsAgreementItem>
                    <FsAgreementItem>
                        <AgreementId>xxxxx</AgreementId>
                        <BPTier1>xx</BPTier1>
                        <BPTier2>xx</BPTier2>
                        <IRSTBuyingProgram>xxxxx</IRSTBuyingProgram>
                        <Product>xxxxx</Product>
                        <ProductId>xxxxx</ProductId>
                    </FsAgreementItem>
                    <FsAgreementItem>
                        <AgreementId>xxxxx</AgreementId>
                        <BPTier1>xx</BPTier1>
                        <BPTier2>xx</BPTier2>
                        <IRSTBuyingProgram>xxxxx</IRSTBuyingProgram>
                        <Product>xxxxx</Product>
                        <ProductId>xxxxx</ProductId>
                    </FsAgreementItem>
                    <FsAgreementItem>
                        <AgreementId>xxxxx</AgreementId>
                        <BPTier1>xx</BPTier1>
                        <BPTier2>xx</BPTier2>
                        <IRSTBuyingProgram>xxxxx</IRSTBuyingProgram>
                        <Product>xxxxx</Product>
                        <ProductId>xxxxx</ProductId>
                    </FsAgreementItem>
                    <FsAgreementItem>
                        <AgreementId>xxxxx</AgreementId>
                        <BPTier1>xx</BPTier1>
                        <BPTier2>xx</BPTier2>
                        <IRSTBuyingProgram>xxxxx</IRSTBuyingProgram>
                        <Product>xxxxx</Product>
                        <ProductId>xxxxx</ProductId>
                    </FsAgreementItem>
                    <FsAgreementItem>
                        <AgreementId>xxxxx</AgreementId>
                        <BPTier1>xx</BPTier1>
                        <BPTier2>xx</BPTier2>
                        <IRSTBuyingProgram>xxxxx</IRSTBuyingProgram>
                        <Product>xxxxx</Product>
                        <ProductId>xxxxx</ProductId>
                    </FsAgreementItem>
                </ListOfFsAgreementItem>
            </ServiceAgreement>
        </ListOfServiceAgreement>
    </Account>
</ListOfIrstAccountBrandAccountResponseIo>

wsdl - 我从文件中删除了端点IP地址。它不是公共服务,但是测试此方案不需要调用该服务。

<?xml version="1.0" encoding="UTF-8"?><definitions
 xmlns="http://schemas.xmlsoap.org/wsdl/"
 xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
 xmlns:xsdLocal1="http://www.siebel.com/xml/IRST%20Account%20Address%20Query%20Request%20IO"
 targetNamespace="http://siebel.com/CustomUI"
 xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
 xmlns:xsdLocal2="http://www.siebel.com/xml/IRST%20Account%20Brand%20Account%20Response%20IO"
 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
 xmlns:tns="http://siebel.com/CustomUI"
><types
><xsd:schema
 elementFormDefault="qualified"
 attributeFormDefault="unqualified"
 targetNamespace="http://www.siebel.com/xml/IRST%20Account%20Brand%20Account%20Response%20IO"
 xmlns:xsdLocal2="http://www.siebel.com/xml/IRST%20Account%20Brand%20Account%20Response%20IO"
 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
><xsd:annotation
><xsd:documentation
>Copyright (C) 2001-2004 Siebel Systems, Inc. All rights reserved. Siebel XSD Generation</xsd:documentation
></xsd:annotation
><xsd:element
 name="ListOfIrstAccountBrandAccountResponseIo"
 type="xsdLocal2:ListOfIrstAccountBrandAccountResponseIo"
></xsd:element
><xsd:complexType
 name="ListOfIrstAccountBrandAccountResponseIoTopElmt"
><xsd:sequence
><xsd:element
 name="ListOfIrstAccountBrandAccountResponseIo"
 maxOccurs="1"
 minOccurs="1"
 type="xsdLocal2:ListOfIrstAccountBrandAccountResponseIo"
></xsd:element
></xsd:sequence
></xsd:complexType
><xsd:complexType
 name="ListOfIrstAccountBrandAccountResponseIo"
><xsd:sequence
><xsd:element
 name="Account"
 maxOccurs="unbounded"
 minOccurs="0"
 type="xsdLocal2:Account"
></xsd:element
></xsd:sequence
></xsd:complexType
><xsd:complexType
 name="Account"
><xsd:sequence
><xsd:element
 name="Id"
 maxOccurs="1"
 minOccurs="0"
 type="xsd:string"
></xsd:element
><xsd:element
 name="AccountStatus"
 maxOccurs="1"
 minOccurs="0"
 type="xsd:string"
></xsd:element
><xsd:element
 name="IRSTAccoutSectorNum"
 maxOccurs="1"
 minOccurs="0"
 type="xsd:string"
></xsd:element
><xsd:element
 name="MainFaxNumber"
 maxOccurs="1"
 minOccurs="0"
 type="xsd:string"
></xsd:element
><xsd:element
 name="MainPhoneNumber"
 maxOccurs="1"
 minOccurs="0"
 type="xsd:string"
></xsd:element
><xsd:element
 name="Name"
 maxOccurs="1"
 minOccurs="1"
 type="xsd:string"
></xsd:element
><xsd:element
 name="ParentAccountId"
 maxOccurs="1"
 minOccurs="0"
 type="xsd:string"
></xsd:element
><xsd:element
 name="ParentAccountName"
 maxOccurs="1"
 minOccurs="0"
 type="xsd:string"
></xsd:element
><xsd:element
 name="AccountRowId"
 maxOccurs="1"
 minOccurs="0"
 type="xsd:string"
></xsd:element
><xsd:element
 name="ListOfCutAddress"
 maxOccurs="1"
 minOccurs="0"
 type="xsdLocal2:ListOfCutAddress"
></xsd:element
><xsd:element
 name="ListOfIrstBrandAccount"
 maxOccurs="1"
 minOccurs="0"
 type="xsdLocal2:ListOfIrstBrandAccount"
></xsd:element
><xsd:element
 name="ListOfInternalDivision"
 maxOccurs="1"
 minOccurs="0"
 type="xsdLocal2:ListOfInternalDivision"
></xsd:element
><xsd:element
 name="ListOfServiceAgreement"
 maxOccurs="1"
 minOccurs="0"
 type="xsdLocal2:ListOfServiceAgreement"
></xsd:element
></xsd:sequence
></xsd:complexType
><xsd:complexType
 name="ListOfCutAddress"
><xsd:sequence
><xsd:element
 name="CutAddress"
 maxOccurs="unbounded"
 minOccurs="0"
 type="xsdLocal2:CutAddress"
></xsd:element
></xsd:sequence
></xsd:complexType
><xsd:complexType
 name="CutAddress"
><xsd:sequence
><xsd:element
 name="City"
 maxOccurs="1"
 minOccurs="1"
 type="xsd:string"
></xsd:element
><xsd:element
 name="Country"
 maxOccurs="1"
 minOccurs="1"
 type="xsd:string"
></xsd:element
><xsd:element
 name="County"
 maxOccurs="1"
 minOccurs="0"
 type="xsd:string"
></xsd:element
><xsd:element
 name="PostalCode"
 maxOccurs="1"
 minOccurs="1"
 type="xsd:string"
></xsd:element
><xsd:element
 name="PrimaryAddressFlg"
 maxOccurs="1"
 minOccurs="0"
 type="xsd:string"
></xsd:element
><xsd:element
 name="State"
 maxOccurs="1"
 minOccurs="1"
 type="xsd:string"
></xsd:element
><xsd:element
 name="StreetAddress"
 maxOccurs="1"
 minOccurs="1"
 type="xsd:string"
></xsd:element
><xsd:element
 name="StreetAddress2"
 maxOccurs="1"
 minOccurs="0"
 type="xsd:string"
></xsd:element
></xsd:sequence
></xsd:complexType
><xsd:complexType
 name="ListOfIrstBrandAccount"
><xsd:sequence
><xsd:element
 name="IrstBrandAccount"
 maxOccurs="unbounded"
 minOccurs="0"
 type="xsdLocal2:IrstBrandAccount"
></xsd:element
></xsd:sequence
></xsd:complexType
><xsd:complexType
 name="IrstBrandAccount"
><xsd:sequence
><xsd:element
 name="Id"
 maxOccurs="1"
 minOccurs="0"
 type="xsd:string"
></xsd:element
><xsd:element
 name="IRSTBrand"
 maxOccurs="1"
 minOccurs="1"
 type="xsd:string"
></xsd:element
><xsd:element
 name="IRSTBrandAccountNumber"
 maxOccurs="1"
 minOccurs="1"
 type="xsd:string"
></xsd:element
><xsd:element
 name="IRSTSIC"
 maxOccurs="1"
 minOccurs="0"
 type="xsd:string"
></xsd:element
><xsd:element
 name="Name"
 maxOccurs="1"
 minOccurs="0"
 type="xsd:string"
></xsd:element
></xsd:sequence
></xsd:complexType
><xsd:complexType
 name="ListOfInternalDivision"
><xsd:sequence
><xsd:element
 name="InternalDivision"
 maxOccurs="unbounded"
 minOccurs="0"
 type="xsdLocal2:InternalDivision"
></xsd:element
></xsd:sequence
></xsd:complexType
><xsd:complexType
 name="InternalDivision"
><xsd:sequence
><xsd:element
 name="Organization"
 maxOccurs="1"
 minOccurs="1"
 type="xsd:string"
></xsd:element
></xsd:sequence
></xsd:complexType
><xsd:complexType
 name="ListOfServiceAgreement"
><xsd:sequence
><xsd:element
 name="ServiceAgreement"
 maxOccurs="unbounded"
 minOccurs="0"
 type="xsdLocal2:ServiceAgreement"
></xsd:element
></xsd:sequence
></xsd:complexType
><xsd:complexType
 name="ServiceAgreement"
><xsd:sequence
><xsd:element
 name="AccountId"
 maxOccurs="1"
 minOccurs="0"
 type="xsd:string"
></xsd:element
><xsd:element
 name="AgreementId"
 maxOccurs="1"
 minOccurs="0"
 type="xsd:string"
></xsd:element
><xsd:element
 name="AgreementNumber"
 maxOccurs="1"
 minOccurs="0"
 type="xsd:string"
></xsd:element
><xsd:element
 name="AgreementStatus"
 maxOccurs="1"
 minOccurs="1"
 type="xsd:string"
></xsd:element
><xsd:element
 name="ListOfFsAgreementItem"
 maxOccurs="1"
 minOccurs="0"
 type="xsdLocal2:ListOfFsAgreementItem"
></xsd:element
></xsd:sequence
></xsd:complexType
><xsd:complexType
 name="ListOfFsAgreementItem"
><xsd:sequence
><xsd:element
 name="FsAgreementItem"
 maxOccurs="unbounded"
 minOccurs="0"
 type="xsdLocal2:FsAgreementItem"
></xsd:element
></xsd:sequence
></xsd:complexType
><xsd:complexType
 name="FsAgreementItem"
><xsd:sequence
><xsd:element
 name="Id"
 maxOccurs="1"
 minOccurs="0"
 type="xsd:string"
></xsd:element
><xsd:element
 name="AgreementId"
 maxOccurs="1"
 minOccurs="0"
 type="xsd:string"
></xsd:element
><xsd:element
 name="BPTier1"
 maxOccurs="1"
 minOccurs="1"
 type="xsd:string"
></xsd:element
><xsd:element
 name="BPTier2"
 maxOccurs="1"
 minOccurs="1"
 type="xsd:string"
></xsd:element
><xsd:element
 name="IRSTBuyingProgram"
 maxOccurs="1"
 minOccurs="0"
 type="xsd:string"
></xsd:element
><xsd:element
 name="Product"
 maxOccurs="1"
 minOccurs="0"
 type="xsd:string"
></xsd:element
><xsd:element
 name="ProductId"
 maxOccurs="1"
 minOccurs="0"
 type="xsd:string"
></xsd:element
></xsd:sequence
></xsd:complexType
></xsd:schema
><xsd:schema
 elementFormDefault="qualified"
 attributeFormDefault="unqualified"
 targetNamespace="http://siebel.com/CustomUI"
 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
><xsd:import
 namespace="http://www.siebel.com/xml/IRST%20Account%20Brand%20Account%20Response%20IO"
></xsd:import
><xsd:import
 namespace="http://www.siebel.com/xml/IRST%20Account%20Address%20Query%20Request%20IO"
></xsd:import
><xsd:element
 name="IRST_spcQuery_spcAccount_spcAnd_spcBrand_spcAccnt_Version_Input"
><xsd:complexType
><xsd:sequence
><xsd:element
 ref="xsdLocal1:ListOfIrstAccountAddressQueryRequestIo"
></xsd:element
></xsd:sequence
></xsd:complexType
></xsd:element
><xsd:element
 name="IRST_spcQuery_spcAccount_spcAnd_spcBrand_spcAccnt_Version_Output"
><xsd:complexType
><xsd:sequence
><xsd:element
 ref="xsdLocal2:ListOfIrstAccountBrandAccountResponseIo"
></xsd:element
><xsd:element
 name="Error_spcCode"
 type="xsd:string"
></xsd:element
><xsd:element
 name="Error_spcMessage"
 type="xsd:string"
></xsd:element
></xsd:sequence
></xsd:complexType
></xsd:element
></xsd:schema
><xsd:schema
 elementFormDefault="qualified"
 attributeFormDefault="unqualified"
 xmlns:xsdLocal1="http://www.siebel.com/xml/IRST%20Account%20Address%20Query%20Request%20IO"
 targetNamespace="http://www.siebel.com/xml/IRST%20Account%20Address%20Query%20Request%20IO"
 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
><xsd:annotation
><xsd:documentation
>Copyright (C) 2001-2004 Siebel Systems, Inc. All rights reserved. Siebel XSD Generation</xsd:documentation
></xsd:annotation
><xsd:element
 name="ListOfIrstAccountAddressQueryRequestIo"
 type="xsdLocal1:ListOfIrstAccountAddressQueryRequestIo"
></xsd:element
><xsd:complexType
 name="ListOfIrstAccountAddressQueryRequestIoTopElmt"
><xsd:sequence
><xsd:element
 name="ListOfIrstAccountAddressQueryRequestIo"
 maxOccurs="1"
 minOccurs="1"
 type="xsdLocal1:ListOfIrstAccountAddressQueryRequestIo"
></xsd:element
></xsd:sequence
></xsd:complexType
><xsd:complexType
 name="ListOfIrstAccountAddressQueryRequestIo"
><xsd:sequence
><xsd:element
 name="Account"
 maxOccurs="unbounded"
 minOccurs="0"
 type="xsdLocal1:Account"
></xsd:element
></xsd:sequence
></xsd:complexType
><xsd:complexType
 name="Account"
><xsd:sequence
><xsd:element
 name="AccountRowId"
 maxOccurs="1"
 minOccurs="0"
 type="xsd:string"
></xsd:element
></xsd:sequence
></xsd:complexType
></xsd:schema
></types
><message
 name="IRST_spcQuery_spcAccount_spcAnd_spcBrand_spcAccnt_Version_Input"
><part
 name="IRST_spcQuery_spcAccount_spcAnd_spcBrand_spcAccnt_Version_Input"
 element="tns:IRST_spcQuery_spcAccount_spcAnd_spcBrand_spcAccnt_Version_Input"
></part
></message
><message
 name="IRST_spcQuery_spcAccount_spcAnd_spcBrand_spcAccnt_Version_Output"
><part
 name="IRST_spcQuery_spcAccount_spcAnd_spcBrand_spcAccnt_Version_Output"
 element="tns:IRST_spcQuery_spcAccount_spcAnd_spcBrand_spcAccnt_Version_Output"
></part
></message
><portType
 name="IRST_spcQuery_spcAccount_spcAnd_spcBrand_spcAccnt_Version"
><operation
 name="IRST_spcQuery_spcAccount_spcAnd_spcBrand_spcAccnt_Version"
><input
 message="tns:IRST_spcQuery_spcAccount_spcAnd_spcBrand_spcAccnt_Version_Input"
></input
><output
 message="tns:IRST_spcQuery_spcAccount_spcAnd_spcBrand_spcAccnt_Version_Output"
></output
></operation
></portType
><binding
 name="IRST_spcQuery_spcAccount_spcAnd_spcBrand_spcAccnt_Version"
 type="tns:IRST_spcQuery_spcAccount_spcAnd_spcBrand_spcAccnt_Version"
><soap:binding
 transport="http://schemas.xmlsoap.org/soap/http"
 style="document"
></soap:binding
><operation
 name="IRST_spcQuery_spcAccount_spcAnd_spcBrand_spcAccnt_Version"
><soap:operation
 soapAction="document/http://siebel.com/CustomUI:IRST_spcQuery_spcAccount_spcAnd_spcBrand_spcAccnt_Version"
></soap:operation
><input
><soap:body
 use="literal"
></soap:body
></input
><output
><soap:body
 use="literal"
></soap:body
></output
></operation
></binding
><service
 name="IRST_spcQuery_spcAccount_spcAnd_spcBrand_spcAccnt_Version"
><port
 binding="tns:IRST_spcQuery_spcAccount_spcAnd_spcBrand_spcAccnt_Version"
 name="IRST_spcQuery_spcAccount_spcAnd_spcBrand_spcAccnt_Version"
><soap:address
 location="http://xx.xx.xx.xx/path/to/service"
></soap:address
></port
></service
></definitions
>

1 个答案:

答案 0 :(得分:0)

似乎我试图使用错误的序列化程序反序列化。以前版本的第三方服务的wsdl与DataContractSerializer一起正常运行。他们实施的更改显然与DataContractSerializersvcutil.exe无法使用XmlSerializer切换为使用XmlSerializer,我不知道。

我转而使用{{1}}进行反序列化,事情终于有效了。