There is an error while deserializing an XML document

I get an error when deserializing an XML document into an object. How can this be solved?

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

This is an 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> 
+4
source share
4 answers

Your XML document is actually three documents. A valid XML document must contain only one root root. In addition, XML declarations are not allowed inside the document.

This is valid XML (XML declaration 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 invalid XML (multiple root elements, xml declaration inside the 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> 
+10
source

To expand on the response of Fredrik MΓΆrk , the key is in the error message: (5, 14) refers to the line number and column number where the parser considers the problem, Here it points to the second XML declaration, which, as mentioned, is not resolved .

+8
source

Add a root element to your Customer elements.

0
source

Try it...

 <?xml version="1.0"?> <ArrayOfCustomer xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <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> 
0
source

Source: https://habr.com/ru/post/1308107/


All Articles