简体   繁体   中英

XML De-serialization giving null values

i am having an XML string like

<?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>

In Order to map the nodes with Class, i am having the class structure like

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

    }

}

For De-serialization , i used xmlserializer to De-serialize the object

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

but the value FullServiceAddressCorrectionDelivery object is always null.. please help me what i am doing wrong here....

Add namesapce on the XmlElement attribute as described here

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM