WCF DataContractSerialzer xml对象不完整的结果

时间:2011-05-25 23:07:53

标签: wcf datacontractserializer

我正在创建一个从另一个Web服务返回xml的wcf包装器服务。 我注意到的是,并非所有来自源的xml都被序列化为我的对象。

这是源xml:

<?xml version="1.0" standalone="yes"?>
<methodResponse>
    <requestId>234843080</requestId>
    <errorCode>0</errorCode>
    <errorText></errorText>
    <processingTime>00:00:00.234</processingTime>
    <results>
        <resultCode>0</resultCode>
        <resultText></resultText>
        <resultCount>1</resultCount>
        <totalResultCount>1</totalResultCount>
        <result>           
            <contactId>123</contactId>
            <sourceFolderId>3443</sourceFolderId>
            <contactState>4</contactState>
            <contactStateSortOrder>3</contactStateSortOrder>            
            <contactType>person</contactType>
            <displayName>Bond, James</displayName>            
            <office>unknown</office>
            <department></department>
            <changeDate></changeDate>
            <firstName>James</firstName>
            <middleName></middleName>
            <lastName>Bond</lastName>            
            <jobTitle>Spy</jobTitle>            
            <ape>
                <address type='Street' typeId='1' relationship='Business' relationshipId='1'>
                    <addressLine></addressLine>                    
                    <city></city>
                    <state></state>
                    <postalCode></postalCode>                                                            
                </address>
                <phone type='Phone' typeId='1' relationship='Business' relationshipId='1'>
                    <phoneNumber></phoneNumber>
                    <description/>
                    <formattedPhone></formattedPhone>
                </phone>         
            </ape>
        </result>
    </results>    
</methodResponse>

wcf包装器服务:

  [DataContract(Name="methodResponse", Namespace = "")]
    public partial class methodResponse {

    [DataMember]
    public int errorCode {
        get;
        set;
    }

    [DataMember]
    public string errorText {
        get;
        set;
    }

    [DataMember]
    public string requestId
    {
        get;
        set;
    }

    [DataMember]
    public methodResponseResults[] results
    {
        get;
        set;
    }
}

[DataContract(Namespace = "")]
public partial class methodResponseResults
{
    [DataMember]
    public string resultCode {
        get;
        set;
    }

    [DataMember]
    public string resultText {
        get;
        set;
    }

    [DataMember]
    public string resultCount {
        get;
        set;
    }

    [DataMember]
    public string totalResultCount {
        get;
        set;
    }

    [DataMember]
    public methodResponseResultsResult[] result {
        get;
        set;
    }
}

[DataContract(Namespace="")]
public partial class methodResponseResultsResult {

public string contactId
        {
            get;
            set;
        }
    public string displayName
    {
        get;
        set;
    }
}



  [ServiceContract]    
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
    public class WcfWrapperService
    {

 public Response GetXml()
        {    
            string uri = ""; // uri to source web service

            var dataContractSerializer = new DataContractSerializer(typeof(methodResponse));

                using (XmlReader reader = XmlReader.Create(uri))
                {
                    var result = (methodResponse)dataContractSerializer.ReadObject(reader);
//result object looks like:    
                //result.errorCode = 0;
                //result.errorText = null;
                //result.requestId = 234843080;
                //result.results = methodResponseResults[0];
                }


    // return Response object
                }
        }

errorCode,errorText,requestId值存在,但没有结果。

2 个答案:

答案 0 :(得分:1)

跳出来的第一件事是你试图取消/序列化包含属性的合同。 DataContractSerializer有一些限制,其中之一是它只能序列化/反序列化元素。为了正确处理此架构,您需要使用XmlSerializer

答案 1 :(得分:0)

您未在Order属性上为DataMember属性设置任何值。这意味着元素将按字母顺序处理,这很可能不是您想要的。您必须明确设置Order以确保它与您要处理的文档的架构相匹配。如果实例文档中可能存在或不存在任何元素,您可能还需要设置IsRequired = false