Sunday, August 10, 2008

xmlns='' was not expected

If you ever run into this XmlSerializer error: it's thrown because the name of the class being deserialized and the root node are not the same.

For example, if you're deserializing XML that looks something like this...
<PersonData>
<FirstName>Bob</FirstName>
<LastName>Black</LastName>
</PersonData>


...into a class called...


public class PersonDataXml
{
...
}


...you'll get this error.

To resolve, just add the XmlRootAttribute to your class, thusly...


[XmlRoot( "PersonData" )]
public class PersonDataXml
{
...
}



Now XmlSerializer won't get all confused.