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

问题描述 投票:0回答:1

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

我在解析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>


c# xml post request restsharp
1个回答
1
投票

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

注意事项:

  1. [soap:Envelope元素上的xmlns上缺少空格。
  2. 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字符串,也许是System.Xml.XmlWriter

© www.soinside.com 2019 - 2024. All rights reserved.