简体   繁体   中英

C# XML - Why does this Code keep failing with 0x3A Error?

i have following code example:

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)
                           )
                        )
                    )
                )
               );

I keep getting the error message in the first line. "plentysoaprequest...."

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

if I comment out those lines, it keeps say it is in the first line of the code.

Edit: the xml should look like this:

<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>

so there seems to be a problem with the xml-sysntax. I cant figure out how to set xsi:type or xsi:type

"oLogin" + xsi + "type" will create a string with value "oLoginhttp://www.w3.org/2001/XMLSchema-instancetype" . That's not a valid name...

You need something a little closer to this:

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")
                   )
                )
            )
        )
       );

Which generates this 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM