Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

There is an error in XML document when deserializing

I'm receiving an error message while deserializing an XML document into an object. How can this be solved?

There is an error in XML document (5, 14)

This is the XML document:

<?xml version="1.0"?>
<Customer xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <FirstName>Khaled</FirstName>
  <LastName>Marouf</LastName>
</Customer><?xml version="1.0"?>
<Customer xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <FirstName>Faisal</FirstName>
  <LastName>Damaj</LastName>
</Customer><?xml version="1.0"?>
<Customer xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <FirstName>Lara</FirstName>
  <LastName>Khalil</LastName>
</Customer>
like image 584
salim Avatar asked Dec 03 '22 05:12

salim


2 Answers

Your XML document is in fact three documents. A valid XML document must have only one root node for instance. Also, XML declarations are not valid inside the document.

This is valid XML (XML declaration comes first, one root element):

<?xml version="1.0"?>
<Customer xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <FirstName>Khaled</FirstName>
  <LastName>Marouf</LastName>
</Customer>

This is not valid XML (multiple root elements, xml declaration inside document):

<?xml version="1.0"?>
<Customer xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <FirstName>Khaled</FirstName>
  <LastName>Marouf</LastName>
</Customer><?xml version="1.0"?>
<Customer xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <FirstName>Faisal</FirstName>
  <LastName>Damaj</LastName>
</Customer>
like image 85
Fredrik Mörk Avatar answered Dec 18 '22 12:12

Fredrik Mörk


To expand on Fredrik Mörk's answer, the clue is in the error message: (5, 14) refers to the row number and column number where the parser thinks the problem is. Here, that points at the second XML declaration, which as has been mentioned is not allowed.

like image 39
AakashM Avatar answered Dec 18 '22 12:12

AakashM