2015-12-30 114 views
0

我目前使用WCF服务来调用外部API。解析布尔XML结果

我有一个方法调用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); 
    } 

用一个简单的类型一样布尔调用它时我为什么会得到这个例外?

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

{ “有XML文档中的误差(1,2)。”}

{“http://schemas.microsoft.com/2003/10/Serialization/'>没有 预期“}

一切有很多复杂的对象

+0

错误信息是复制/粘贴的吗?因为''''''''中存在不匹配的问题 –

+0

似乎是(1,2) - >'“中的小文本引起了问题。看看你是否可以通过使'''一致来解决,也可以在'Serialization /'后面加上'(注意引号) – Ian

+0

我得到一个错误,这个值为'xmlns = \“http://schemas.microsoft.com/ 2003/10/Serialization/\“>'但没有这个'xmlns =”http://schemas.microsoft.com/2003/10/Serialization/“>'。你知道一个解决方法谁工作?使用'response.Content.Replace(@“\”,“”)'没有帮助我仍然有例外 –

回答

1

的工作完美这应该工作 -

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