0

I'm using Refit to consume an XML ReST Web Api. There is some issue with XML deserialization into POCOs (which are annotated with attributes from System.Xml.Serialization) since I get a NullReferenceException in Refit.RequestBuilderImplementation.DeserializeContentAsync.

However, I a no further information on how to debug the issue, since the exception message tells me nothing on what attribute or object in the response is not adapted to my POCOs.

The XMl is :

<?xml version="1.0" encoding="utf-16"?>
<MediaContainer friendlyName="myPlex" identifier="com.plexapp.plugins.myplex" machineIdentifier="c62be90a37d43ecd9596be8abd98a2e485979e31" size="2">
   <Server accessToken="plexauthtoken" name="VOSTOK" address="ip address" port="1" version="1.23.0.4497-a1b1f3c10" scheme="http" host="ip address" localAddresses="ipaddress1,ipaddress2" machineIdentifier="machineid" createdAt="1590584258" updatedAt="1621708154" owned="1" synced="0" />
</MediaContainer>
Ombrelin
  • 547
  • 1
  • 8
  • 27
  • 1
    Xml serialization is hard to debug. Normally I start by commenting out in the c# classes all the object in the root class. And then add back in one at a time until I locate the property that is causing the issue. – jdweng May 24 '21 at 15:06
  • I tried that but even with 1 or even 0 attributes in my root class I get the exception so I was looking for a way to have a helpful error message – Ombrelin May 24 '21 at 15:35
  • The indicates an namespace issue. You need to add before the root class : [XmlRoot(Namespace = "The URL of the namespace")] – jdweng May 24 '21 at 15:40
  • Mmh but my XML is just a response from a ReST API, it doesn't have a namespace. I'll edit to add the xml for reference – Ombrelin May 24 '21 at 18:15

1 Answers1

-2

Net library doesn't like "utf-16". Solution is to skip first line of xml like this :

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string xml = "<?xml version=\"1.0\" encoding=\"utf-16\"?>\n<MediaContainer>\n</MediaContainer>";
            StringReader sReader = new StringReader(xml);
            sReader.ReadLine();
            XmlReader xReader = XmlReader.Create(sReader);
            XmlSerializer serializer = new XmlSerializer(typeof(MediaContainer));
            MediaContainer MediaContainer = (MediaContainer)serializer.Deserialize(xReader); 
        }
    }
    public class MediaContainer
    {
    }
}
jdweng
  • 33,250
  • 2
  • 15
  • 20