3

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

Syed Salman Raza Zaidi
  • 2,172
  • 9
  • 42
  • 87

3 Answers3

6

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; }
}
Ondrej Svejdar
  • 21,349
  • 5
  • 54
  • 89
0

The node is named UserInformation, while the class is named UserInfo. You can either rename the class or use suitable attributes to configure its serialized name. You can look at this tutorial for more details on controlling serialization with attributes.

Codor
  • 17,447
  • 9
  • 29
  • 56
  • What happens if you use `XmlElementAttribute` instead of `DataMemberAttribute`? – Codor May 04 '15 at 13:12
  • I am getting this: Attribute 'DataMemberAttribute' is not valid on this declaration type. It is only valid on 'property, indexer, field' declarations: Well let me check your link too – Syed Salman Raza Zaidi May 04 '15 at 13:15
0

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();
Community
  • 1
  • 1
JoeJoe87577
  • 512
  • 3
  • 17