2012-06-05 152 views
2

我有一個像WCF隨着XmlSerializer的

<?xml version="1.0"?> 
<FullServiceAddressCorrectionDelivery xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <AuthenticationInfo xmlns="http://www.usps.com/postalone/services/UserAuthenticationSchema"> 
    <UserId xmlns="">FAPushService</UserId> 
    <UserPassword xmlns="">Password4Now</UserPassword> 
    </AuthenticationInfo> 
    <FullServiceAddressCorrectionDelivery d2p1:MessageGroupID="3599" d2p1:TotalMessageCount="1" d2p1:MessageSerialNumber="1" xmlns:d2p1="http://idealliance.org/Specs/mailxml10.0/mailxml_defs" xmlns="http://idealliance.org/Specs/mailxml10.0A/mailxml_dd"> 
    <SubmittingParty d2p1:MaildatUserLicense="USPS" /> 
    <SubmittingSoftware d2p1:SoftwareName="PostalOne" d2p1:Vendor="USPS" d2p1:Version="27.0" /> 
    <PushMessageID>3599001</PushMessageID> 
    </FullServiceAddressCorrectionDelivery> 
</FullServiceAddressCorrectionDelivery> 

和像

[ServiceContract(Namespace = http://www.usps.com/postalone/services/DataDistributionPushMailXML10A")] 
public interface DataDistributionPushMailXML10APortType 
{ 
    [OperationContract] 
    [XmlSerializerFormat] 
    FullServiceAddressCorrectionDelivery PushFullServiceAddressCorrectionDelivery(FullServiceAddressCorrectionDelivery obj); 
} 

// Use a data contract as illustrated in the sample below to add composite types to service operations. 
[DataContract(Name = "FullServiceAddressCorrectionDelivery", Namespace = "http://www.w3.org/2001/XMLSchema-instance")] 
[Serializable] 
public class FullServiceAddressCorrectionDelivery 
{ 
    [XmlElement("AuthenticationInfo", 
    Namespace = "http://www.usps.com/postalone/services/UserAuthenticationSchema")] 
    [DataMember] 
    public AuthenticationInfoType AuthenticationInfo 
    { 
     get; 
     set; 
    } 

    [XmlElement("FullServiceAddressCorrectionDelivery", 
    Namespace = "http://idealliance.org/Specs/mailxml10.0A/mailxml_dd")] 
    [DataMember] 
    public FullServiceAddressCorrectionDelivery1 FullServiceAddressCorrectionDelivery1 
    { 
     get; 
     set; 
    } 
} 

[DataContract] 
[Serializable] 
public class AuthenticationInfoType 
{ 
    [DataMember] 
    [XmlElement("UserId", Namespace = "")] 
    public string UserId 
    { 
     get; 
     set; 
    } 

    [DataMember] 
    [XmlElement("UserPassword", Namespace = "")] 

    public string UserPassword 
    { 
     get; 
     set; 
    } 
} 

[DataContract] 
[Serializable] 
public class ParticipantIDType 
{ 
    private string _maildatUserLicense; 

    [DataMember] 
    [XmlAttribute("MaildatUserLicense", 
    Namespace ="http://idealliance.org/Specs/mailxml10.0/mailxml_defs")] 
    public string MaildatUserLicense 
    { 
     get 
     { 
      return this._maildatUserLicense; 
     } 
     set 
     { 
      this._maildatUserLicense = value; 
     } 
    } 
} 

[DataContract] 
[Serializable] 
public class FullServiceAddressCorrectionDelivery1 
{ 
    [DataMember] 
    [XmlElement("SubmittingParty")] 
    public ParticipantIDType SubmittingParty 
    { 
     get; 
     set; 
    } 

    [DataMember] 
    [XmlElement("PushMessageID")] 
    public string PushMessageID 
    { 
     get; 
     set; 

    } 
} 

在反序列化的服務合同的XML字符串,我用XmlSerializer

FullServiceAddressCorrectionDelivery objAddressCorrectionDelivery = 
    new FullServiceAddressCorrectionDelivery(); 
byte[] byteArray = System.Text.Encoding.ASCII.GetBytes(this.richTextBox1.Text); 
MemoryStream stream = new MemoryStream(byteArray); 
XmlSerializer Xs = new XmlSerializer(typeof(FullServiceAddressCorrectionDelivery)); 
var result = (FullServiceAddressCorrectionDelivery)Xs.Deserialize(stream); 

問題是反序列化:我FullServiceAddressCorrectionDelivery對象 已成功反序列化,但它包含在其PushMessageID屬性中爲null。我錯過了什麼?

回答

2

原因是您的FullServiceAddressCorrectionDelivery1類(內部FullServiceAddressCorrectionDelivery)不包含SubmittingSoftware元素的定義,但XML包含它。 接下來會發生什麼是XML解串器可以在提交軟件之前讀取屬性,但是在SubmittingSoftware之後的所有屬性都設置爲空。 我自己在從Web服務反序列化XML中遇到過這個問題。

通常情況下,如果您使用Web服務的版本1創建客戶端,然後創建Web服務的版本2,並在數據合同中擁有一個屬性,則會發生這種情況。 在這些情況下,您需要確保新屬性放置在XML的最後(底部)。

+0

非常感謝你GTG!你想出了確切的問題..現在正在做恩典... .. :) – Hassam