0

我有以下代码示例:

PlentySoapRequest_GetAuthentificationToken username = new PlentySoapRequest_GetAuthentificationToken();

        username.Username = user_textbox.ToString();
        username.Userpass = password_textbox.ToString();

        Uri uri = new Uri("http://www.****.de/plenty/api/soap/version105/");


        XNamespace soapenv = @"http://schemas.xmlsoap.org/soap/envelope/";
        XNamespace xsi = @"http://www.w3.org/2001/XMLSchema-instance";
        XNamespace xsd = @"http://www.w3.org/2001/XMLSchema";
        XNamespace ver = @"http://www.****.de/plenty/api/soap/version105/";

        var document = new XDocument(
           new XDeclaration("1.0", "utf-8", String.Empty),
           new XElement(soapenv + "Envelope",
               new XAttribute(XNamespace.Xmlns + "xsi", xsi),
               new XAttribute(XNamespace.Xmlns + "xsd" , xsd),
               new XAttribute(XNamespace.Xmlns + "soapenv" , soapenv),
               new XAttribute(XNamespace.Xmlns + "ver" , ver),
           new XElement(soapenv + "Header"),
           new XElement(soapenv + "Body",
                   new XElement(ver + "GetAuthentificationToken",
                        new XElement("oLogin" + xsi + "type" + ver + "PlentySoapRequest_GetAuthentificationToken",
                            new XAttribute("Username" + xsi + "type" + xsd + "string", username.Username),
                            new XAttribute("Userpass" + xsi + "type" + xsd + "string", username.Userpass)
                           )
                        )
                    )
                )
               );

我一直在第一行收到错误消息。“大量的请求……”

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

如果我注释掉这些行,它会一直说它在代码的第一行。

编辑: xml 应该如下所示:

<soapenv:Body>
  <ver:GetAuthentificationToken>
     <oLogin xsi:type="ver:PlentySoapRequest_GetAuthentificationToken">
        <!--You may enter the following 2 items in any order-->
        <Username xsi:type="xsd:string">apitest</Username>
        <Userpass xsi:type="xsd:string">apitest</Userpass>
     </oLogin>
  </ver:GetAuthentificationToken>

所以xml-sysntax似乎有问题。我不知道如何设置xsi:typexsi:type

4

1 回答 1

5

"oLogin" + xsi + "type"将创建一个带有 value 的字符串"oLoginhttp://www.w3.org/2001/XMLSchema-instancetype"。这不是一个有效的名字...

你需要一些更接近这个的东西:

var document = new XDocument(
   new XDeclaration("1.0", "utf-8", String.Empty),
   new XElement(soapenv + "Envelope",
       new XAttribute(XNamespace.Xmlns + "xsi", xsi),
       new XAttribute(XNamespace.Xmlns + "xsd", xsd),
       new XAttribute(XNamespace.Xmlns + "soapenv", soapenv),
       new XAttribute(XNamespace.Xmlns + "ver", ver),
   new XElement(soapenv + "Header"),
   new XElement(soapenv + "Body",
           new XElement(ver + "GetAuthentificationToken",
                new XElement(xsi + "Login", 
                    new XAttribute(xsi + "type", "blahblah"),
                    new XElement("Username",
                        new XAttribute(xsi + "type", "xsd:string"),
                                "myUserName")
                   )
                )
            )
        )
       );

生成此 XML

<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ver="http://www.****.de/plenty/api/soap/version105/">
  <soapenv:Header />
  <soapenv:Body>
    <ver:GetAuthentificationToken>
      <xsi:Login xsi:type="blahblah">
        <Username xsi:type="xsd:string">myUserName</Username>
      </xsi:Login>
    </ver:GetAuthentificationToken>
  </soapenv:Body>
</soapenv:Envelope>
于 2012-08-07T11:19:00.037 回答