Removing XML deserialization into a class

I have XML that I de-serialize, This is my XML

<?xml version=\"1.0\" encoding=\"utf-16\"?>
<UserInfo xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'>  
<UserName>First_User</UserName>  
<Age>25</Age>  
</UserInfo>

      

I have this class

namespace MyProject{
    [System.SerializableAttribute()]
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "http:/MyProject.ServiceContracts/2006/11", IsNullable = false)]

    [DataContract]
    public class UserInfo
    {
       private string username;

        private string age;

        [DataMember]
        public string UserName
        {
            get
            {
                return this.username;
            }
            set
            {
                this.username = value;
            }
        }

           [DataMember]
        public string Age
        {
            get
            {
                return this.age;
            }
            set
            {
                this.age = value;
            }
        }

    }
    }

      

and i do it

     XmlSerializer xmlSerSale = new XmlSerializer(typeof(UserInfo));

                        StringReader stringReader = new StringReader(myXML);

                        info = (UserInfo)xmlSerSale.Deserialize(stringReader);

                        stringReader.Close();

      

This gives me an exception,

{"<UserInformation xmlns=''> was not expected."}

      

How can I fix this? Any alternative way to fix this? I have to use this in WebService

+3


source to share


3 answers


You declare the namespace in the xml meta descriptors:

[XmlRoot(Namespace = "http:/MyProject.ServiceContracts/2006/11", IsNullable = false)]
public class UserInfo
{
  [XmlElement]
  public string UserName { get; set; }
  [XmlElement]
  public string Age { get; set; }
}

      

When you do this, you should also have this namespace in your xml:

var foo = @"<?xml version=""1.0"" encoding=""utf-16""?>
<UserInfo xmlns='http:/MyProject.ServiceContracts/2006/11' 
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' 
xmlns:xsd='http://www.w3.org/2001/XMLSchema'>  
<UserName>First_User</UserName>  
<Age>25</Age>  
</UserInfo>";

var xmlSerSale = new XmlSerializer(typeof(UserInfo));
using (var stringReader = new StringReader(foo))
{
  var info = (UserInfo)xmlSerSale.Deserialize(stringReader);
}    

      



Also note that attributes are [DataContract]

[DataMember]

ignored XmlSerializer

.

UPDATE: If you cannot change the xml, then you need to drop the namespace descriptor from the attribute XmlRoot

. I.e:.

[XmlRoot(IsNullable = false)]
public class UserInfo
{
  [XmlElement]
  public string UserName { get; set; }
  [XmlElement]
  public string Age { get; set; }
}

      

+5


source


node has a name UserInformation

and class is UserInfo

. You can rename the class, or use appropriate attributes to customize its serialized name. You can take a look at this tutorial for more details on controlling serialization with attributes.



0


source


Check this answer For your class it would be:

[XmlRoot("Userinfo")]
public class UserInfo
{
    [XmlElement("UserName")]
    public string Username { get; set; }
    [XmlElement("Age")]
    public string Age { get; set; }
}

      

and

UserInfo info;
XmlSerializer serializer = new XmlSerializer(typeof(UserInfo));
StreamReader reader = new StreamReader(filePath);
info = (UserInfo)serializer.Deserialize(reader);
reader.Close();

      

0


source







All Articles