首页 > 解决方案 > RestSharp 请求中的错误?, XML , ':' 字符,十六进制值 0x3A,不能包含在名称中

问题描述

:字符 十六进制值0x3A不能包含在名称中。

POST在解析 RestSharp请求中的 XML 正文时,我从 API 收到上述错误。

我能做什么?

  string xmlBody = "<soap:Envelope" +
                      " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" +
                      " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"" +
                      " xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\">" +
                      "<soap:Body> " +
                          "<MainField " +
                               " xmlns =\"http://www.w3.org\">" +
                               "<Username>string</Username> " +
                               "<Password>string</Password> " +
                               "<FieldPlace> " +
                                   "<Value1>string</Value1> " +
                                   "<Value2>string</Value2> " +  
                               "</FieldPlace> " +
                          "</MainField> " +
                      "</soap:Body> " +
                   "</soap:Envelope>";

  requestPost.AddParameter("text/xml", xmlBody, "text/xml" , ParameterType.RequestBody);

这是 XML


<soap:Envelope
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
    <soap:Body>
        <MainField>
              xmlns ="http://www.w3.org">
            <Username>string</Username>
            <Password>string</Password>
            <FieldPlace>
                <Value1>string</Value1>
                <Value2>string</Value2>
            </FieldPlace>
        </MainField>
    </soap:Body>
</soap:Envelope>


标签: c#xmlpostrequestrestsharp

解决方案


请尝试使用此更正的 XML,并让我知道这是否适合您。

需要注意的几点:

    1. .xmlns 中缺少空格soap:Envelope element
    1. MainField元素在添加命名空间之前已关闭。
string xmlBody = "<soap:Envelope" +
                 " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" +
                 " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"" +
                 " xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\">" +
                     "<soap:Body> " +
                         "<MainField " +
                              "xmlns=\"http://www.w3.org/2003/05/soap-envelope\" > " +
                              "<Username>string</Username> " +
                              "<Password>string</Password> " +
                              "<FieldPlace> " +
                                  "<Value1>string</Value1> " +
                                  "<Value2>string</Value2> " +
                              "</FieldPlace> " +
                         "</MainField> " +
                     "</soap:Body> " +
                  "</soap:Envelope>";

我还建议使用另一种方法来生成 XML string,也许是System.Xml.XmlWriter.


推荐阅读