Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deserialize XML to Class

Tags:

c#

xml

I have XML which I am de-serializing,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 am doing this

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

                        StringReader stringReader = new StringReader(myXML);

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

                        stringReader.Close();

Its giving me exception,

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

how can I fix this?Any alternate way to fix this? I have to use this in WebService

like image 641
Syed Salman Raza Zaidi Avatar asked May 28 '26 11:05

Syed Salman Raza Zaidi


1 Answers

You declare the namespace in your 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 also have to 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 the [DataContract] [DataMember] attributes are ignored by XmlSerializer.

UPDATE: if you can't change the xml you have to drop the namespace descriptor from XmlRoot attribute. I.E.:

[XmlRoot(IsNullable = false)]
public class UserInfo
{
  [XmlElement]
  public string UserName { get; set; }
  [XmlElement]
  public string Age { get; set; }
}
like image 61
Ondrej Svejdar Avatar answered May 31 '26 04:05

Ondrej Svejdar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!