RESTSharp有反序列化XML包括字节顺序标记的问题? [英] RESTSharp has problems deserializing XML including Byte Order Mark?
问题描述
有一个公共Web服务,我想在很短的C#应用程序的使用方法:
http://ws.parlament.ch/
There is a public webservice which I want to use in a short C# Application: http://ws.parlament.ch/
这是此WebService的返回的XML有一个物料清单开头,这将导致RESTSharp失败,并出现以下错误信息的XML的反序列化:
The returned XML from this webservice has a "BOM" at the beginning, which causes RESTSharp to fail the deserializing of the XML with the following error message:
错误检索响应。检查内部细节的更多信息。 --->
System.Xml.XmlException:是在根级别的数据无效。在System.Xml.XmlTextReaderImpl.Throw(例外五)在System.Xml.XmlTextReaderImpl.Throw结果
(字符串资源,字符串ARG)在$ 1号线,
位置1。 b $ b System.Xml.XmlTextReaderImpl.ParseRootLevelWhitespace()留在
System.Xml.Linq的在
System.Xml.XmlTextReaderImpl.Read
System.Xml.XmlTextReaderImpl.ParseDocumentContent()() .XDocument.Load(XmlReader中的读者,LoadOptions期权)在System.Xml.Linq.XDocument.Parse
(字符串文本,LoadOptions选项),结果
在System.Xml.Linq.XDocument.Parse(在
RestSharp.Deserializers.XmlDeserializer.Deserialize [T]字符串文本)(IRestResponse $在RestSharp.RestClient.Deserialize [T] b $ b响应)(IRestRequest
要求,IRestResponse生)结果
---电内部异常堆栈跟踪---
Error retrieving response. Check inner details for more info. ---> System.Xml.XmlException: Data at the root level is invalid. Line 1, position 1. at System.Xml.XmlTextReaderImpl.Throw(Exception e)
at System.Xml.XmlTextReaderImpl.Throw(String res, String arg) at System.Xml.XmlTextReaderImpl.ParseRootLevelWhitespace() at System.Xml.XmlTextReaderImpl.ParseDocumentContent() at System.Xml.XmlTextReaderImpl.Read() at System.Xml.Linq.XDocument.Load(XmlReader reader, LoadOptions options) at System.Xml.Linq.XDocument.Parse(String text, LoadOptions options)
at System.Xml.Linq.XDocument.Parse(String text) at RestSharp.Deserializers.XmlDeserializer.Deserialize[T](IRestResponse response) at RestSharp.RestClient.Deserialize[T](IRestRequest request, IRestResponse raw)
--- End of inner exception stack trace ---
下面是使用的http://ws.parlament.ch/sessions?format=xml 得到的一个List 会话的
Here is an easy sample by using http://ws.parlament.ch/sessions?format=xml to get a List of 'Sessions':
public class Session
{
public int Id { get; set; }
public DateTime? Updated { get; set; }
public int? Code { get; set; }
public DateTime? From { get; set; }
public string Name { get; set; }
public DateTime? To { get; set; }
}
static void Main(string[] args)
{
var request = new RestRequest();
request.RequestFormat = DataFormat.Xml;
request.Resource = "sessions";
request.AddParameter("format", "xml");
var client = new RestClient("http://ws.parlament.ch/");
var response = client.Execute<List<Session>>(request);
if (response.ErrorException != null)
{
const string message = "Error retrieving response. Check inner details for more info.";
var ex = new ApplicationException(message, response.ErrorException);
Console.WriteLine(ex);
}
List<Session> test = response.Data;
Console.Read();
}
当我第一次操作使用Fiddler返回的XML除去前3位(在物料清单),上面的代码工作!可能有人请帮我直接在RESTSharp处理呢?我究竟做错了什么? !谢谢你在前进
When I first manipulate the returned xml with Fiddler to remove the first 3 bits (the "BOM"), the above code works! May someone please help me to handle this directly in RESTSharp? What am I doing wrong? THANK YOU in advance!
推荐答案
我找到了解决方案 - !谢谢@arootbeer的提示
I found the Solution - Thank you @arootbeer for the hints!
相反包裹XMLDeserializer,你还可以使用#RESTSharp了RestRequest.OnBeforeDeserialization事件。所以,你只需要在新RestRequest后插入这样的事情()(请参阅我最初的代码示例),然后它的作品完美!
Instead of wrapping the XMLDeserializer, you can also use the 'RestRequest.OnBeforeDeserialization' event from #RESTSharp. So you just need to insert something like this after the new RestRequest() (see my initial code example) and then it works perfect!
request.OnBeforeDeserialization = resp =>
{
//remove the first ByteOrderMark
//see: http://stackoverflow.com/questions/19663100/restsharp-has-problems-deserializing-xml-including-byte-order-mark
string byteOrderMarkUtf8 = Encoding.UTF8.GetString(Encoding.UTF8.GetPreamble());
if (resp.Content.StartsWith(byteOrderMarkUtf8))
resp.Content = resp.Content.Remove(0, byteOrderMarkUtf8.Length);
};
这篇关于RESTSharp有反序列化XML包括字节顺序标记的问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!