来自Goodreads API的Xml反序列化错误(System.InvalidOperationException)

时间:2017-06-24 07:48:15

标签: c# api xml-deserialization

我正在使用goodreads api构建一个应用程序。我运行了以下URI: 的 https://www.goodreads.com/book/title.xml?key=Mykey&title=pride%20and%20prejudice

并获得了Xml文档:

<GoodreadsResponse>
<Request>
<authentication>true</authentication>
<key>
<![CDATA[ MyKey ]]>
</key>
<method>
<![CDATA[ book_title ]]>
</method>
</Request>
<book>
.....
</book>
</GoodreadsResponse>

然后我复制了文档并做了一个特殊的粘贴(粘贴为xml类)来生成模型。

我为模型编写了一个服务类,如下所示:

   public class RecommendationService
{
    public static async Task<Models.RecommendationItem.GoodreadsResponse> GetRecommendationInfo(string title)
    {
        string uri = $"https://www.goodreads.com/book/title.xml?key=my key&title={title}";
        var bookItem = new Models.RecommendationItem.GoodreadsResponse();

        using (var client = new WebClient())
        {
            HttpWebRequest request = WebRequest.Create(uri) as HttpWebRequest;

            XmlSerializer serializer = new XmlSerializer(typeof(Models.RecommendationItem.GoodreadsResponse));
            WebResponse response = request.GetResponse();
            bookItem = (Models.RecommendationItem.GoodreadsResponse)serializer.Deserialize(response.GetResponseStream());
            bookItem.book.title = bookItem.book.title ?? "FAIL";

        }
        return bookItem;
    }

当我为标题运行时,这段代码就像一个魅力一样骄傲和偏见&#34;但是当我竞选标题&#34; fire and ice&#34;时,我得到错误(带有堆栈跟踪):

System.InvalidOperationException:XML文档中存在错误(10,4)。 ---&GT; System.OverflowException:对于UInt16,值太大或太小。在System.Xml.XmlConvert.ToUInt16的System.UInt16.Parse(String s,NumberStyles样式,NumberFormatInfo信息)\ r \ n中,值太大或太小。(字符串s)\ r \ n在Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderGoodreadsResponse.Read31_GoodreadsResponseBook(Boolean isNullable,Boolean checkType)\ r \ n在Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderGoodreadsResponse.Read32_GoodreadsResponse(Boolean isNullable,Boolean checkType)\ r \ n \ n在Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderGoodreadsResponse.Read33_GoodreadsResponse()\ r \ n ---内部异常堆栈跟踪结束--- \ r \ n在System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader,在C:\ Users \ Sravani中的CystaTLB.Services.RecommendationService.d__0.MoveNext()处的System.Xml.Serialization.XmlSerializer.Deserialize(Stream stream)\ r \ n中的String encodingStyle,XmlDeserializationEvents事件)\ r \ n \ Documents \ Visual Studio 2015 \ Projects \ CystaTLB \ CystaTLB \ Services \ RecommendationService.cs:第27行\ r \ n ---从抛出异常的上一个位置开始的堆栈跟踪结束--- \ r \ n在System.Runtime .CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)\ r \ n在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\ r \ n在System.Runtime.CompilerServices.TaskAwaiter 1.GetResult()\r\n at CystaTLB.CystaLuisDialog.<RB>d__7.MoveNext() in C:\\Users\\Sravani\\Documents\\Visual Studio 2015\\Projects\\CystaTLB\\CystaTLB\\CystaLuisDialog.cs:line 200\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at Microsoft.Bot.Builder.Dialogs.LuisDialog 1.d__6.MoveNext ()\ r \ n ---从抛出异常的上一个位置开始的堆栈跟踪结束---在System.Runtime的System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)\ r \ n中的\ r \ n。 CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\ r \ n在Microsoft.Bot.Builder.Dialogs.Internals.DialogTask.ThunkResume 1.<Rest>d__4.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at Microsoft.Bot.Builder.Internals.Fibers.Wait 2.-PollAsync&gt; d__19.MoveNext()\ r \ n ---结束抛出异常的前一个位置的堆栈跟踪---在System.Runt的System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)\ r \ n中的\ r \ n Microsoft.Bot.Builder.Internals.Fibers.Frame 1.<Microsoft-Bot-Builder-Internals-Fibers-IFrameLoop<C>-PollAsync>d__7.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at Microsoft.Bot.Builder.Internals.Fibers.Fiber中的ime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\ r \ n 1.-PollAsync&gt; d__13.MoveNext()\ r \ n ---结束抛出异常的前一个位置的堆栈跟踪---在Microsoft.Bot.Builder.Internals.Fibers.Wait 2.Microsoft.Bot.Builder.Internals.Fibers.IAwaiter<T>.GetResult()\r\n at Microsoft.Bot.Builder.Dialogs.Chain.LoopDialog的System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\ r \ n中的\ r \ n 1.d__3.MoveNext()\ r \ n ---从抛出异常的上一个位置开始的堆栈跟踪结束--- \ r \ n在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)\ r \ n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\ r \ n在Microsoft.Bot.Builder.Dialogs.Internals.DialogTask.ThunkResume 1.<Rest>d__4.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at Microsoft.Bot.Builder.Internals.Fibers.Wait 2.-PollAsync&gt; d__19.MoveNext()\ r \ n ---从抛出异常的先前位置开始的堆栈跟踪结束---在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)\ r \ n处于System.Runtime.CompilerServices.TaskAwaiter.Handle处的\ r \ n NonSuccessAndDebuggerNotification(任务任务)\ r \ n在Microsoft.Bot.Builder.Internals.Fibers.Frame 1.<Microsoft-Bot-Builder-Internals-Fibers-IFrameLoop<C>-PollAsync>d__7.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at Microsoft.Bot.Builder.Internals.Fibers.Fiber 1.-PollAsync&gt; d__13.MoveNext()\ r \ n --- ---从先前位置开始的堆栈跟踪抛出异常的地方--- \ r \ n在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)\ r \ n,在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\ r \ n在Microsoft。 Bot.Builder.Dialogs.Internals.DialogTask.d__21 1.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at Microsoft.Bot.Builder.Dialogs.Internals.ReactiveDialogTask.<Microsoft-Bot-Builder-Dialogs-Internals-IPostToBot-PostAsync>d__5 1.MoveNext()\ r \ n ---从抛出异常的上一个位置的堆栈跟踪结束--- \ r \ n在System.Runtime .CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)\ r \ n在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\ r \ n在Microsoft.Bot.Builder.Dialogs.Internals.LocalizedDialogTask.d__2 {{1} 1.d__6 1.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at Microsoft.Bot.Builder.Dialogs.Internals.ScoringDialogTask 1.MoveNext()\ r \ n ---从抛出异常的上一个位置开始的堆栈跟踪结束---在Microsoft.Bot.Builder.Dialogs.Internals中的\ r \ n。 PersistentDialog Task.d__6`1.MoveNext()\ r \ n ---从抛出异常的上一个位置开始的堆栈跟踪结束--- \ r \ n在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)\ r \ n \ n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\ r \ n在Microsoft.Bot.Builder.Dialogs.Conversation.d__4.MoveNext()\ r \ n ---从上一个位置的堆栈跟踪结束抛出异常的地方--- \ r \ n在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)\ r \ n,在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\ r \ n在Microsoft。 Bot.Builder.Dialogs.Conversation.d__2.MoveNext()\ r \ n ---从抛出异常的上一个位置开始的堆栈跟踪结束--- \ r \ n在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)\ r \ n在System.Runtime.CompilerServices上的System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\ r \ n .TaskAwaiter.GetResult()\ r \ n在C:\ Users \ Sravani \ Documents \ Visual Studio 2015 \ Projects \ CystaTLB \ CystaTLB \ Controllers \ MessagesController.cs中的CystaTLB.MessagesController.d__0.MoveNext():第31行和第34行;

我做错了什么? 有没有办法可以解决这个问题?

0 个答案:

没有答案