InvalidOperationException:不应<xml xmlns =''>

时间:2019-07-16 07:40:13

标签: c# xml restsharp webapi2

作为一个模拟项目,我让一个控制器向另一个控制器发送请求,在该控制器中,发送者模拟外部API。

我正在使用restsharp库,因为我可以更好地了解异常消息,并且对我来说更容易使用。

请求将发送到另一个端点,如下所示:

string url = $"{BaseUrl}/api/orders/post3";
string rawXml = "<?xml version='1.0' encoding='UTF-8'?><xml><quote>26</quote></xml>";

var client = new RestClient(url);


var request = new RestRequest()
            {
                RequestFormat = DataFormat.Xml,
                XmlSerializer = new RestSharp.Serializers.DotNetXmlSerializer(),
                Method = Method.POST,
                XmlNamespace = null

            };

request.AddXmlBody(rawXml);
var response = client.Post(request);

这是抛出的完整异常:

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta charset="utf-8" />
        <title>Internal Server Error</title>
        <style>
            body {
    font-family: 'Segoe UI', Tahoma, Arial, Helvetica, sans-serif;
    font-size: .813em;
    color: #222;
    background-color: #fff;
}

h1, h2, h3, h4, h5 {
    /*font-family: 'Segoe UI',Tahoma,Arial,Helvetica,sans-serif;*/
    font-weight: 100;
}

h1 {
    color: #44525e;
    margin: 15px 0 15px 0;
}

h2 {
    margin: 10px 5px 0 0;
}

h3 {
    color: #363636;
    margin: 5px 5px 0 0;
}

code {
    font-family: Consolas, "Courier New", courier, monospace;
}

body .titleerror {
    padding: 3px 3px 6px 3px;
    display: block;
    font-size: 1.5em;
    font-weight: 100;
}

body .location {
    margin: 3px 0 10px 30px;
}

#header {
    font-size: 18px;
    padding: 15px 0;
    border-top: 1px #ddd solid;
    border-bottom: 1px #ddd solid;
    margin-bottom: 0;
}

    #header li {
        display: inline;
        margin: 5px;
        padding: 5px;
        color: #a0a0a0;
        cursor: pointer;
    }

    #header .selected {
        background: #44c5f2;
        color: #fff;
    }

#stackpage ul {
    list-style: none;
    padding-left: 0;
    margin: 0;
    /*border-bottom: 1px #ddd solid;*/
}

#stackpage .details {
    font-size: 1.2em;
    padding: 3px;
    color: #000;
}

#stackpage .stackerror {
    padding: 5px;
    border-bottom: 1px #ddd solid;
}


#stackpage .frame {
    padding: 0;
    margin: 0 0 0 30px;
}

    #stackpage .frame h3 {
        padding: 2px;
        margin: 0;
    }

#stackpage .source {
    padding: 0 0 0 30px;
}

    #stackpage .source ol li {
        font-family: Consolas, "Courier New", courier, monospace;
        white-space: pre;
        background-color: #fbfbfb;
    }

#stackpage .frame .source .highlight li span {
    color: #FF0000;
}

#stackpage .source ol.collapsible li {
    color: #888;
}

    #stackpage .source ol.collapsible li span {
        color: #606060;
    }

.page table {
    border-collapse: separate;
    border-spacing: 0;
    margin: 0 0 20px;
}

.page th {
    vertical-align: bottom;
    padding: 10px 5px 5px 5px;
    font-weight: 400;
    color: #a0a0a0;
    text-align: left;
}

.page td {
    padding: 3px 10px;
}

.page th, .page td {
    border-right: 1px #ddd solid;
    border-bottom: 1px #ddd solid;
    border-left: 1px transparent solid;
    border-top: 1px transparent solid;
    box-sizing: border-box;
}

    .page th:last-child, .page td:last-child {
        border-right: 1px transparent solid;
    }

.page .length {
    text-align: right;
}

a {
    color: #1ba1e2;
    text-decoration: none;
}

    a:hover {
        color: #13709e;
        text-decoration: underline;
    }

.showRawException {
    cursor: pointer;
    color: #44c5f2;
    background-color: transparent;
    font-size: 1.2em;
    text-align: left;
    text-decoration: none;
    display: inline-block;
    border: 0;
    padding: 0;
}

.rawExceptionStackTrace {
    font-size: 1.2em;
}

.rawExceptionBlock {
    border-top: 1px #ddd solid;
    border-bottom: 1px #ddd solid;
}

.showRawExceptionContainer {
    margin-top: 10px;
    margin-bottom: 10px;
}

.expandCollapseButton {
    cursor: pointer;
    float: left;
    height: 16px;
    width: 16px;
    font-size: 10px;
    position: absolute;
    left: 10px;
    background-color: #eee;
    padding: 0;
    border: 0;
    margin: 0;
}

        </style>
    </head>
    <body>
        <h1>An unhandled exception occurred while processing the request.</h1>
            <div class="titleerror">InvalidOperationException: &lt;xml xmlns=&#x27;&#x27;&gt; was not expected.</div>
                <p class="location">System.Xml.Serialization.XmlSerializationPrimitiveReader.Read_double()</p>
            <div class="titleerror">InvalidOperationException: There is an error in XML document (1, 33).</div>
                <p class="location">System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, string encodingStyle, XmlDeserializationEvents events)</p>
        <ul id="header">
            <li id="stack" tabindex="1" class="selected">
                Stack
            </li>
            <li id="query" tabindex="2">
                Query
            </li>
            <li id="cookies" tabindex="3">
                Cookies
            </li>
            <li id="headers" tabindex="4">
                Headers
            </li>
        </ul>

        <div id="stackpage" class="page">
            <ul>
                                    <li>
                        <h2 class="stackerror">InvalidOperationException: &lt;xml xmlns=&#x27;http://www.w3.org/&#x27;&gt; was not expected.</h2>
                        <ul>
                            <li class="frame" id="frame1">
                                    <h3>System.Xml.Serialization.XmlSerializationPrimitiveReader.Read_double()</h3>

                            </li>
                            <li class="frame" id="frame2">
                                    <h3>System.Xml.Serialization.XmlSerializer.DeserializePrimitive(XmlReader xmlReader, XmlDeserializationEvents events)</h3>

                            </li>
                            <li class="frame" id="frame3">
                                    <h3>System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, string encodingStyle, XmlDeserializationEvents events)</h3>

                            </li>
                        </ul>
                    </li>
                    <li>
                        <br/>
                        <div class="rawExceptionBlock">
                            <div class="showRawExceptionContainer">
                                <button class="showRawException" data-exceptionDetailId="exceptionDetail1">Show raw exception details</button>
                            </div>
                            <div id="exceptionDetail1" class="rawExceptionDetails">
                                <pre class="rawExceptionStackTrace">System.InvalidOperationException: &lt;xml xmlns=&#x27;http://www.w3.org/&#x27;&gt; was not expected.&#xD;&#xA;   at System.Xml.Serialization.XmlSerializationPrimitiveReader.Read_double()&#xD;&#xA;   at System.Xml.Serialization.XmlSerializer.DeserializePrimitive(XmlReader xmlReader, XmlDeserializationEvents events)&#xD;&#xA;   at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)</pre>
                            </div>
                        </div>
                    </li>
                    <li>
                        <h2 class="stackerror">InvalidOperationException: There is an error in XML document (1, 33).</h2>
                        <ul>
                            <li class="frame" id="frame4">
                                    <h3>System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, string encodingStyle, XmlDeserializationEvents events)</h3>

                            </li>
                            <li class="frame" id="frame5">
                                    <h3>System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader)</h3>

                            </li>
                            <li class="frame" id="frame6">
                                    <h3>Microsoft.AspNetCore.Mvc.Formatters.XmlSerializerInputFormatter.ReadRequestBodyAsync(InputFormatterContext context, Encoding encoding)</h3>

                            </li>
                            <li class="frame" id="frame7">
                                    <h3>Microsoft.AspNetCore.Mvc.ModelBinding.Binders.BodyModelBinder.BindModelAsync(ModelBindingContext bindingContext)</h3>

                            </li>
                            <li class="frame" id="frame8">
                                    <h3>Microsoft.AspNetCore.Mvc.ModelBinding.ParameterBinder.BindModelAsync(ActionContext actionContext, IModelBinder modelBinder, IValueProvider valueProvider, ParameterDescriptor parameter, ModelMetadata metadata, object value)</h3>

                            </li>
                            <li class="frame" id="frame9">
                                    <h3>Microsoft.AspNetCore.Mvc.Internal.ControllerBinderDelegateProvider&#x2B;&lt;&gt;c__DisplayClass0_0&#x2B;&lt;&lt;CreateBinderDelegate&gt;g__Bind|0&gt;d.MoveNext()</h3>

                            </li>
                            <li class="frame" id="frame10">
                                    <h3>Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeInnerFilterAsync()</h3>

                            </li>
                            <li class="frame" id="frame11">
                                    <h3>Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter()</h3>

                            </li>
                            <li class="frame" id="frame12">
                                    <h3>Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context)</h3>

                            </li>
                            <li class="frame" id="frame13">
                                    <h3>Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)</h3>

                            </li>
                            <li class="frame" id="frame14">
                                    <h3>Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync()</h3>

                            </li>
                            <li class="frame" id="frame15">
                                    <h3>Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync()</h3>

                            </li>
                            <li class="frame" id="frame16">
                                    <h3>Microsoft.AspNetCore.Routing.EndpointMiddleware.Invoke(HttpContext httpContext)</h3>

                            </li>
                            <li class="frame" id="frame17">
                                    <h3>Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.Invoke(HttpContext httpContext)</h3>

                            </li>
                            <li class="frame" id="frame18">
                                    <h3>Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)</h3>

                            </li>
                        </ul>
                    </li>
                    <li>
                        <br/>
                        <div class="rawExceptionBlock">
                            <div class="showRawExceptionContainer">
                                <button class="showRawException" data-exceptionDetailId="exceptionDetail2">Show raw exception details</button>
                            </div>
                            <div id="exceptionDetail2" class="rawExceptionDetails">
                                <pre class="rawExceptionStackTrace">System.InvalidOperationException: There is an error in XML document (1, 33). ---&gt; System.InvalidOperationException: &lt;xml xmlns=&#x27;http://www.w3.org/&#x27;&gt; was not expected.&#xD;&#xA;   at System.Xml.Serialization.XmlSerializationPrimitiveReader.Read_double()&#xD;&#xA;   at System.Xml.Serialization.XmlSerializer.DeserializePrimitive(XmlReader xmlReader, XmlDeserializationEvents events)&#xD;&#xA;   at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)&#xD;&#xA;   --- End of inner exception stack trace ---&#xD;&#xA;   at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)&#xD;&#xA;   at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader)&#xD;&#xA;   at Microsoft.AspNetCore.Mvc.Formatters.XmlSerializerInputFormatter.ReadRequestBodyAsync(InputFormatterContext context, Encoding encoding)&#xD;&#xA;   at Microsoft.AspNetCore.Mvc.ModelBinding.Binders.BodyModelBinder.BindModelAsync(ModelBindingContext bindingContext)&#xD;&#xA;   at Microsoft.AspNetCore.Mvc.ModelBinding.ParameterBinder.BindModelAsync(ActionContext actionContext, IModelBinder modelBinder, IValueProvider valueProvider, ParameterDescriptor parameter, ModelMetadata metadata, Object value)&#xD;&#xA;   at Microsoft.AspNetCore.Mvc.Internal.ControllerBinderDelegateProvider.&lt;&gt;c__DisplayClass0_0.&lt;&lt;CreateBinderDelegate&gt;g__Bind|0&gt;d.MoveNext()&#xD;&#xA;--- End of stack trace from previous location where exception was thrown ---&#xD;&#xA;   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeInnerFilterAsync()&#xD;&#xA;   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter()&#xD;&#xA;   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context)&#xD;&#xA;   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(State&amp; next, Scope&amp; scope, Object&amp; state, Boolean&amp; isCompleted)&#xD;&#xA;   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync()&#xD;&#xA;   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync()&#xD;&#xA;   at Microsoft.AspNetCore.Routing.EndpointMiddleware.Invoke(HttpContext httpContext)&#xD;&#xA;   at Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.Invoke(HttpContext httpContext)&#xD;&#xA;   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)</pre>
                            </div>
                        </div>
                    </li>
            </ul>
        </div>

        <div id="querypage" class="page">
                <p>No QueryString data.</p>
        </div>

        <div id="cookiespage" class="page">
                <p>No cookie data.</p>
        </div>
        <div id="headerspage" class="page">
                <table>
                    <thead>
                        <tr>
                            <th>Variable</th>
                            <th>Value</th>
                        </tr>
                    </thead>
                    <tbody>
                                <tr>
                                    <td>Accept</td>
                                    <td>application/json, text/json, text/x-json, text/javascript, application/xml, text/xml</td>
                                </tr>
                                <tr>
                                    <td>Accept-Encoding</td>
                                    <td>gzip, deflate</td>
                                </tr>
                                <tr>
                                    <td>Connection</td>
                                    <td>Keep-Alive</td>
                                </tr>
                                <tr>
                                    <td>Content-Length</td>
                                    <td>93</td>
                                </tr>
                                <tr>
                                    <td>Content-Type</td>
                                    <td>application/xml</td>
                                </tr>
                                <tr>
                                    <td>Host</td>
                                    <td>localhost:11373</td>
                                </tr>
                                <tr>
                                    <td>User-Agent</td>
                                    <td>RestSharp/106.6.10.0</td>
                                </tr>
                    </tbody>
                </table>
        </div>
        <script>
            //<!--
            (function (window, undefined) {
    "use strict";

    function ns(selector, element) {
        return new NodeCollection(selector, element);
    }

    function NodeCollection(selector, element) {
        this.items = [];
        element = element || window.document;

        var nodeList;

        if (typeof (selector) === "string") {
            nodeList = element.querySelectorAll(selector);
            for (var i = 0, l = nodeList.length; i < l; i++) {
                this.items.push(nodeList.item(i));
            }
        }
    }

    NodeCollection.prototype = {
        each: function (callback) {
            for (var i = 0, l = this.items.length; i < l; i++) {
                callback(this.items[i], i);
            }
            return this;
        },

        children: function (selector) {
            var children = [];

            this.each(function (el) {
                children = children.concat(ns(selector, el).items);
            });

            return ns(children);
        },

        hide: function () {
            this.each(function (el) {
                el.style.display = "none";
            });

            return this;
        },

        toggle: function () {
            this.each(function (el) {
                el.style.display = el.style.display === "none" ? "" : "none";
            });

            return this;
        },

        show: function () {
            this.each(function (el) {
                el.style.display = "";
            });

            return this;
        },

        addClass: function (className) {
            this.each(function (el) {
                var existingClassName = el.className,
                    classNames;
                if (!existingClassName) {
                    el.className = className;
                } else {
                    classNames = existingClassName.split(" ");
                    if (classNames.indexOf(className) < 0) {
                        el.className = existingClassName + " " + className;
                    }
                }
            });

            return this;
        },

        removeClass: function (className) {
            this.each(function (el) {
                var existingClassName = el.className,
                    classNames, index;
                if (existingClassName === className) {
                    el.className = "";
                } else if (existingClassName) {
                    classNames = existingClassName.split(" ");
                    index = classNames.indexOf(className);
                    if (index > 0) {
                        classNames.splice(index, 1);
                        el.className = classNames.join(" ");
                    }
                }
            });

            return this;
        },

        attr: function (name) {
            if (this.items.length === 0) {
                return null;
            }

            return this.items[0].getAttribute(name);
        },

        on: function (eventName, handler) {
            this.each(function (el, idx) {
                var callback = function (e) {
                    e = e || window.event;
                    if (!e.which && e.keyCode) {
                        e.which = e.keyCode; // Normalize IE8 key events
                    }
                    handler.apply(el, [e]);
                };

                if (el.addEventListener) { // DOM Events
                    el.addEventListener(eventName, callback, false);
                } else if (el.attachEvent) { // IE8 events
                    el.attachEvent("on" + eventName, callback);
                } else {
                    el["on" + type] = callback;
                }
            });

            return this;
        },

        click: function (handler) {
            return this.on("click", handler);
        },

        keypress: function (handler) {
            return this.on("keypress", handler);
        }
    };

    function frame(el) {
        ns(".source .collapsible", el).toggle();
    }

    function expandCollapseButton(el) {
        var frameId = el.getAttribute("data-frameId");
        frame(document.getElementById(frameId));
        if (el.innerText === "+") {
            el.innerText = "-";
        }
        else {
            el.innerText = "+";
        }
    }

    function tab(el) {
        var unselected = ns("#header .selected").removeClass("selected").attr("id");
        var selected = ns("#" + el.id).addClass("selected").attr("id");

        ns("#" + unselected + "page").hide();
        ns("#" + selected + "page").show();
    }

    ns(".rawExceptionDetails").hide();
    ns(".collapsible").hide();
    ns(".page").hide();
    ns("#stackpage").show();

    ns(".expandCollapseButton")
        .click(function () {
            expandCollapseButton(this);
        })
        .keypress(function (e) {
            if (e.which === 13) {
                expandCollapseButton(this);
            }
        });

    ns("#header li")
        .click(function () {
            tab(this);
        })
        .keypress(function (e) {
            if (e.which === 13) {
                tab(this);
            }
        });

    ns(".showRawException")
        .click(function () {
            var exceptionDetailId = this.getAttribute("data-exceptionDetailId");
            ns("#" + exceptionDetailId).toggle();
        });
})(window);
            //-->
        </script>
    </body>
</html>

我已经浏览过多个帖子,但都出现了相同的错误,但是没有一个帮助。

但是,我还必须提到,接收方控制器还处理来自其他控制器的其他请求,这些请求工作正常。

非常感谢!

0 个答案:

没有答案