繁体   English   中英

将XML反序列化为类

[英]Deserialize XML to Class

我有XML,我正在反序列化,这是我的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."}

我该如何解决这个问题呢?还有其他方法可以解决这个问题 我必须在WebService中使用它

您在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);
}    

另请注意, XmlSerializer会忽略[DataContract] [DataMember]属性。

更新:如果您无法更改xml,则必须从XmlRoot属性中删除命名空间描述符。 IE:

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

该节点名为UserInformation ,而该类名为UserInfo 您可以重命名该类,也可以使用合适的属性来配置其序列化名称。 您可以查看本教程 ,了解有关使用属性控制序列化的更多详细信息。

检查此答案对于您的课程,它将是:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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