分析布尔XML结果

本文关键字:结果 XML 布尔 | 更新日期: 2023-09-27 18:28:37

我当前正在使用WCF服务来调用外部API。

我有一个方法调用Ping(),用于检查外部API是否可用并准备好被调用。

[OperationContract]
bool Ping();

我从外部API得到以下结果:

<boolean xmlns='"http://schemas.microsoft.com/2003/10/Serialization/'">true</boolean>

错误出现在我的xmlns元素中。事实上,这个值运行良好:

<boolean toto='"http://schemas.microsoft.com/2003/10/Serialization/'">true</boolean>

我有以下通用的反序列化方法:

public static T Deserialize<T>(IRestResponse response)
    {
        var serializer = new XmlSerializer(typeof(T));
        var reader = new StringReader(response.Content);
        return (T)serializer.Deserialize(reader);
    }

为什么我在用类似boolean的简单类型调用它时会出现这个异常?

var res = XmlHelper.Deserialize<bool>(client.Execute(request));

{"XML文档(1,2)中存在错误。"}

{"http://schemas.microsoft.com/2003/10/Serialization/'>不是预期。"}

所有东西都能完美地与许多复杂的对象配合使用

分析布尔XML结果

这应该可以工作-

var xml = new StreamReader("xmlPath");
var t = new XmlSerializer(typeof(Boolean),"http://schemas.microsoft.com/2003/10/Serialization/");
var o = t.Deserialize(xml); // true