XML Payload gives 'The ' ' character, hexadecimal value 0x20, cannot be included in a name.' error



  • I'm attempting to create a test framework in c# to automate some of our API tests. Relatively new to this area so have come across an issue I can't solve. My XML payload is as below:

     <Order>
       <BillingAddress>
         <AddressLine1></AddressLine1>
         <AddressLine2></AddressLine2>
         <City></City>
       </BillingAddress>
      <Items>
        <Item IsKit="false">
          <CountryVATRate></CountryVATRate>
          <Group></Group>
          <Reference></Reference>
     </Item>
     </Items>
    </Order>
    

    When I attempt to execute the above(using RestSharp) I get the below error: "System.Xml.XmlException: 'The ' ' character, hexadecimal value 0x20, cannot be included in a name.'"

    The offending line of code is

            <Item IsKit="false">
    

    ...where the code in c# I have written is as below

            XElement items = new XElement("Items");
            items.Add(new XElement("Item IsKit =\"false\""
    

    I believe the issue is because there are spaces between the words 'Item' and 'IsKit'....my question is how I would get around this in order for this element to be added to my test code?



  • you are trying to make an element called 'Item IsKit =\"false\"'

    it looks like you actually want to create an Element called Item with an attribute called IsKit.

    Try this:

    new XElement("Item",new XAttribute("IsKit", "False"))

    Docs are here https://docs.microsoft.com/en-us/dotnet/api/system.xml.linq.xattribute



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2