Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 :

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

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;
    }
like image 582
user572652 Avatar asked May 07 '26 02:05

user572652


1 Answers

There is a default xml namespace; you can fix this by adding [XmlRoot] to tell it the namespace to expect:

[XmlRoot(Namespace="http://schemas.datacontract.org/2004/07/iEverydayLog.Models.ViewModel")]
public class UserInfoView {...}
like image 58
Marc Gravell Avatar answered May 09 '26 16:05

Marc Gravell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!