c# - WCF 与 XmlSerializer

标签 c# wcf xml-serialization

我有一个类似

的 XML 字符串
<?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>

和服务契约(Contract)如

[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;

    }
}

在反序列化时,我使用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。我错过了什么?

最佳答案

原因是您的 FullServiceAddressCorrectionDelivery1 类(内部 FullServiceAddressCorrectionDelivery)不包含 SubmittingSoftware 元素的定义,但 XML 包含它。 然后似乎会发生的是,XML 反序列化器可以读取 SubmittingSoftware 之前的属性,但 SubmittingSoftware 之后的所有属性都设置为 null。 我在从 Web 服务反序列化 XML 时亲身经历过这一点。

如果您使用 Web 服务的版本 1 创建客户端,然后创建 Web 服务的版本 2,并在数据契约(Contract)中多了一个属性,通常会发生这种情况。 在这些情况下,您需要确保新属性位于 XML 的最后(底部)。

关于c# - WCF 与 XmlSerializer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10893244/

相关文章:

c# - 将 Model3DGroup 的多个实例添加到 ModelVisual3D

c# - 竞争对手的应用程序安装到与我相同的文件夹,不需要管理员权限

c# - 提供类作为保存数据的服务的正确方法是什么?

c# - 在 ServiceHost 中运行的 WCF 服务(控制台应用程序)

c# - 如何在xml中嵌入xml

c# - 如何在linq中获得不同的值(value)

c# - 如何为 WlanHostedNetworkSetProperty 函数设置 C# 结构

wcf - 如何使用 Autofac 使用 WCF 服务?

.net - 反序列化缺失整数时不同的 .NET XML 序列化器行为

C# 将 XML 序列化为对象