开发者

There is an error in XML document when deserializing

开发者 https://www.devze.com 2022-12-28 19:52 出处:网络
I\'m receiving an error message while deserializing an XML document into an object. How can this be solved?

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="htt开发者_StackOverflow中文版p://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>


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>


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.


Add a root element for your Customer elements.


Try this...

<?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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号