找到你要的答案

Q:Deserialize XML to Class

Q:反序列化的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

我有XML,我是de serializing,这是我的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>

我有这个班

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

    }
    }

我这样做

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

                        StringReader stringReader = new StringReader(myXML);

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

                        stringReader.Close();

它给我例外,

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

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

answer1: 回答1:

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

您声明xml元描述符中的命名空间:

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

当你这样做时,你也必须在你的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);
}    

同时注意[ ] [ ] DataContract属性被忽略由XmlSerializer DataMember。

更新:如果你不能改变你必须从XmlRoot属性下降的命名空间的XML描述符。即.:

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

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.

节点为用户信息,而类命名为UserInfo。您可以重命名类或使用合适的属性来配置其序列化的名字。您可以在本教程中查看有关使用属性控制序列化的更多细节。

answer3: 回答3:

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

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

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