简体   繁体   English

xml反序列化问题

[英]xml deserialization problem

i have a problem when i trying to use XmlSerializer to deserialize an xml file into a class i made myself, the xml file content is : 我有一个问题,当我尝试使用XmlSerializer将xml文件反序列化为我自己创建的类时,xml文件内容为:

<UserInfoView xmlns="http://schemas.datacontract.org/2004/07/iEverydayLog.Models.ViewModel" 
              xmlns:i="http://www.w3.org/2001/XMLSchema-instance">

    <Address>Ultimo NSW</Address>

    <Id>685c7109-56c5-4a69-82d6-c9a286225d7e</Id>

    <Name>Tester Name</Name>

    <NumberOfCards>1</NumberOfCards>

</UserInfoView>

it failed with this message : There is an error in XML document (1, 2). 它失败了这条消息:XML文档中存在错误(1,2)。

but when i remove these words: 但是当我删除这些词:

xmlns="http://schemas.datacontract.org/2004/07/iEverydayLog.Models.ViewModel"     xmlns:i="http://www.w3.org/2001/XMLSchema-instance"

it works ok, does any one know what happens here? 它运作正常,有没有人知道这里发生了什么?

my class is defined like this: 我的类定义如下:

    public class UserInfoView
    {
        public string Address;

        public string Name;

        public string Id;

        public int NumberOfCards;
    }

There is a default xml namespace; 有一个默认的xml命名空间; you can fix this by adding [XmlRoot] to tell it the namespace to expect: 你可以通过添加[XmlRoot]来告诉它期望的命名空间来解决这个问题:

[XmlRoot(Namespace="http://schemas.datacontract.org/2004/07/iEverydayLog.Models.ViewModel")]
public class UserInfoView {...}

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

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