Xml deserialization issue

I have a problem when I try to use the XmlSerializer to deserialize the xml file into a class that I made myself, the contents of the xml file:

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

failed with this message: there is an error in the XML document (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 fine, does anyone know what is going on here?

my class is defined as follows:

    public class UserInfoView
    {
        public string Address;

        public string Name;

        public string Id;

        public int NumberOfCards;
    }
+3
source share
1 answer

There is a default xml namespace; you can fix this by adding [XmlRoot]to say that the namespace expects:

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

Source: https://habr.com/ru/post/1784947/


All Articles