0

I am stuck deserializing the returned XML from a RESTful API call. This is the error message I am getting back:

System.AggregateException : One or more errors occurred. ----> System.Runtime.Serialization.SerializationException : Error in line 1 position 106. Expecting element 'ArrayOfAPIUtility.PartInfo' from namespace 'http://schemas.datacontract.org/2004/07/MyProject.Web'.. Encountered 'Element' with name 'Part', namespace ''.

I followed this stackoverflow answer to create a successful REST connection.

The returned XML looks like this:

<Part>
    <ItemId>12345</ItemId>
    <ItemDescription>Item Description</ItemDescription>
    <Cost>190.59</Cost>
    <Weight>0.5</Weight>
</Part>

I am trying to to deserialize it like this:

public class PartInfo
{
    public string ItemId { get; set; }
    public string ItemDescription { get; set; }
    public string Cost { get; set; }
    public string Weight { get; set; }
}

public void GetPartInfo(string itemId)
{
    var URL = ...some URL...;
    client.BaseAddress = new Uri(URL);

    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xml"));

    HttpResponseMessage response = client.GetAsync(urlParameters).Result;
    if (response.IsSuccessStatusCode)
    {
        var dataObjects = response.Content.ReadAsAsync<IEnumerable<PartInfo>>().Result;
        foreach (var d in dataObjects)
        {
            Console.WriteLine("{0}", d.ItemId);
        }
    }
}

The result is the error message pasted above.

I think I am missing something very elementary here :-)

Thank you very much for your help!

Community
  • 1
  • 1
jrn
  • 2,640
  • 4
  • 29
  • 51
  • 1
    I suspect it is because your className "PartInfo" does not match Tagname "Part". I guess you either have to annotate the class, so XML-Serialization will know it has to expect the TagName "Part" or you rename the class to "Part". – Fildor Mar 23 '17 at 16:50
  • Thank you for your response @Fildor! How would I annotate the class, so that XML-Serialization will know it has to expect the TagName "Part"? – jrn Mar 23 '17 at 17:54
  • Even if I change the class name to `public class Part` I am still getting the same error message as pasted above. @Fildor – jrn Mar 23 '17 at 18:06

1 Answers1

0

Try xml linq

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication48
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);  //use parse instead if input is a string
            PartInfo partInfo = doc.Elements("Part").Select(x => new PartInfo()
            {
                ItemId = (string)x.Element("ItemId"),
                ItemDescription = (string)x.Element("ItemDescription"),
                Cost = (decimal)x.Element("Cost"),
                Weight = (double)x.Element("Weight")
            }).FirstOrDefault();

        }
    }
    public class PartInfo
    {
        public string ItemId { get; set; }
        public string ItemDescription { get; set; }
        public decimal Cost { get; set; }
        public double Weight { get; set; }
    }
}
jdweng
  • 33,250
  • 2
  • 15
  • 20