RestSharp 请求出错? , XML , ':' 字符, 十六进制值 0x3A, 不能包含在名称中

Error in RestSharp Request? , XML , The ':' character, hexadecimal value 0x3A, cannot be included in a name

The : character , hexadecimal value 0x3A, cannot be included in a name.

我在解析 RestSharp POST 请求中的 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>


请尝试使用这个更正后的 XML,让我知道它是否适合你。

两点注意事项:

    1. soap:Envelope element 处的 xmlns 中缺少空格。
    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.