views:

461

answers:

4

why I get There is an error in XML document (5, 14) while Iam trying to Deserialize an XML document.?

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"&gt;
  <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"&gt;
  <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"&gt;
  <FirstName>Lara</FirstName>
  <LastName>Khalil</LastName>
</Customer>

iam trying to Deserialize the XML to array of customer objects.

+4  A: 

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"&gt;
  <FirstName>Khaled</FirstName>
  <LastName>Marouf</LastName>
</Customer>

This is not (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"&gt;
  <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"&gt;
  <FirstName>Faisal</FirstName>
  <LastName>Damaj</LastName>
</Customer>
Fredrik Mörk
A: 

Add a root element for your Customer elements.

24x7Programmer
+1  A: 

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.

AakashM
A: 

Try this...

<?xml version="1.0"?>
<ArrayOfCustomer xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
  <Customer>
    <FirstName>Khaled</FirstName>
    <LastName>Marouf</LastName>
  </Customer>
  <Customer>
    <FirstName>Faisal</FirstName>
    <LastName>Damaj</LastName>
  </Customer>
  <Customer>
    <FirstName>Lara</FirstName>
    <LastName>Khalil</LastName>
  </Customer>
</ArrayOfCustomer>
digiguru