I just faced an issue while de-serializing a XML file to specific type or object class. Actually normal XML file without Namespace gets de-serialized easily but when you de-serialize XML with Namespace it gives following error

“There is an error in XML document”

Resolution is very simple actually you just need to include Namespace with XMLSerialization Class and exception will go away.


XmlSerializer serializer = new
XmlSerializer(typeof(Books), http://schemas.com/BooksSchema”);


Books books = (Books)serializer.Deserialize(new System.IO.FileStream(“XmlDeserializationSample.xml”,System.IO.FileMode.OpenOrCreate));

<?xml
version=1.0
encoding=utf-8 ?>

<ns1:Books
xmlns:ns1=http://schemas.com/BooksSchema>

<ns1:Book>

<ns1:Title>Professional.ASP.NET.MVC.2</ns1:Title>

<ns1:Author>Scott Hanselman</ns1:Author>

</ns1:Book>

</ns1:Books>

Advertisements