我正在使用重新安装来使用XML ReST Web Api。由于我在Refit.RequestBuilderImplementation.DeserializeContentAsync
中得到了NullReferenceException
,所以将XML反序列化为POCO(用System.Xml.Serialization
中的属性进行注释(存在一些问题。
然而,我没有关于如何调试问题的进一步信息,因为异常消息告诉我响应中的哪个属性或对象不适合我的POCO。
XMl是:
<?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>
Net库不喜欢"utf-16";。解决方案是跳过xml的第一行,如下所示:
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
{
}
}