XML : Parsing/Deserialize XML data from API into object - There is an error in XML document (1871, 60)

I am working with an XML api, which returns the following:

  <inventory>      <product inventoryId="1722474"  externalReference="SM" site="global" total="0" allocated="0" available="0" frozen="0" onOrder="0" lastStockChangeId="505401" lastLineRequirementChangeId="0"/>      <product inventoryId="1722476"  externalReference="PM" site="global" total="0" allocated="0" available="0" frozen="0" onOrder="0" lastStockChangeId="243256" lastLineRequirementChangeId="0"/>      .... 100s of nodes ....  </inventory>    

So, from this returned xml nodes, I am only interested in the following fields/attributes externalReference and available.

Therefore; I created the following class to describe the xml content I am going to deserialize/parse:

  [XmlRoot(ElementName = "product")]  public class StockLevelProduct  {      [XmlAttribute(AttributeName = "externalReference")]      public string ExternalReference { get; set; }        [XmlAttribute(AttributeName = "available")]      public string Available { get; set; }  }    [XmlRoot(ElementName = "inventory")]  public class StockLevelResult  {      [XmlElement(ElementName = "product")]      public List<StockLevelProduct> Product { get; set; }  }    

Then I put it all together like this:

  // Init  StockLevelResult stockLevelResult;    // Anticipate errors  try  {      // Generate request url      string requestUrl = string.Format("{0}/remotewarehouse/inventory.xml?channel={1}",          apiUrl,          apiChannel);        // Call api      string apiResultXmlString = ApiGet(requestUrl);        // Fix api result xml string      if (!apiResultXmlString.Contains("<?xml"))          apiResultXmlString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" + apiResultXmlString;        // Deserialize xml string to object      stockLevelResult = XmlParser.Parse<StockLevelResult>(apiResultXmlString);  }  catch (Exception ex)  {      Console.WriteLine('Failed to download stock levels - ' + ex.Message);  }    

Note* the returned xml string from the API server does not contain <?xml version="1.0" encoding="UTF-8"?> so I manually add it.

When this code executes; I get the following exception being thrown:

There is an error in XML document (1871, 60)

Any ideas why this isn't working? Is it an issue with the returned XML string? or the way I am trying to parse/deserialize it?

No comments:

Post a Comment