0

I'm trying to get some data from a WebService using SOAP request. The SOAP body should contain an SQL query. Whenever I'm using the < character it causes the above mentioned error at SOAPReqBody.LoadXml(). How can I fix this?

HttpWebRequest request = CreateSOAPWebRequest();
XmlDocument SOAPReqBody = new XmlDocument();
SOAPReqBody.LoadXml(@"<?xml version=""1.0"" encoding=""utf-8""?>
 <soap:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"">
 <soap:Body>  
    <Query xmlns=""http://LifaOIS.DK/OISService"">
      <UID>" + uid + @"</UID>
      <PWD>" + pwd + @"</PWD>
      <SQL>" + sql + @"</SQL>
      <Meta>" + meta + @"</Meta>
     </Query>
  </soap:Body>  
</soap:Envelope>");
Bogdan
  • 23,890
  • 3
  • 69
  • 61
compsci-78
  • 3
  • 1
  • 2
  • The code posted is correcrt proviced the variables uid, pwd, sql, and meta do not contain illegal xml characters. See : https://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references. Look only at the 5 xml special characters. – jdweng Mar 30 '21 at 08:38

2 Answers2

0

This is ugly but if you really have to carry a SQL query within the message payload and the less than '<' character is a problem within the XML and you can't control packing and unpacking of the payload, then you might like to consider using BETWEEN...

WHERE field NOT EQUAL minValue AND field BETWEEN minvalue AND upperValue

Note that BETWEEN's from and to values are inclusive - hence the need for the exclusion of minValue

AlanK
  • 1,827
  • 13
  • 16
0

Have you tried wrapping your sql in <![CDATA[...]]>?

Like:

<SQL><![CDATA[" + sql + @"]]></SQL>

Instead of

<SQL>" + sql + @"</SQL>

See What does <![CDATA[]]> in XML mean? for more details.

Bogdan
  • 23,890
  • 3
  • 69
  • 61