1

我有一个 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>

为了用类映射节点,我有像这样的类结构

 [Serializable]
public class FullServiceAddressCorrectionDelivery
{
    [XmlElement("AuthenticationInfo")]
    public AuthenticationInfo AuthenticationInfo
    {
        get;
        set;
    }

}

[Serializable]
public class AuthenticationInfo 
{
    [XmlElement("UserId")]
    public string UserId
    {
        get;
        set;

    }
    [XmlElement("UserPassword")]
    public string UserPassword
    {
        get;
        set;

    }

}

对于反序列化,我使用 xmlserializer 反序列化对象

        byte[] byteArray = System.Text.Encoding.ASCII.GetBytes(xmlString);
        MemoryStream stream = new MemoryStream(byteArray);
        XmlSerializer xs = new XmlSerializer(typeof(FullServiceAddressCorrectionDelivery));
        var result = (FullServiceAddressCorrectionDelivery)xs.Deserialize(stream);

但值 FullServiceAddressCorrectionDelivery 对象始终为空..请帮助我在这里做错了什么....

4

1 回答 1

0

如此处所述,在 XmlElement 属性上添加命名空间

    [Serializable]
    public class FullServiceAddressCorrectionDelivery
    {
        [XmlElement("AuthenticationInfo", 
              Namespace = 
              "http://www.usps.com/postalone/services/UserAuthenticationSchema")]
        public AuthenticationInfo AuthenticationInfo
        {
            get;
            set;
        }
    }

    [Serializable]
    public class AuthenticationInfo
    {
        [XmlElement("UserId", Namespace="")]
        public string UserId
        {
            get;
            set;
        }
        [XmlElement("UserPassword", Namespace = "")]
        public string UserPassword
        {
            get;
            set;
        }
    } 
于 2012-06-03T12:10:50.327 回答