Adding xml to an ‘Any’ Element

Sometimes you will need to add an XML fragment into another XML document. A good example of this would be while constructing a SOAP request.

A simple solution to this would be to just build a string value and use the ToXml() method to add the xml fragment from your object model.

E.g. This C# example shows inserting an XML message part into a OpenTravel webservice SOAP request:

string soap = "<?xml version=\"1.0\" encoding=\"utf-8\"?><SOAP-ENV:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"><SOAP-ENV:Body><tns:OTA_VehLocSearch xmlns:tns=\"http://www.opentravel.org/OTA/2003/05\" xmlns=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">";

soap = soap + liquidObj.ToXml();

soap = soap + "</tns:OTA_VehLocSearch></SOAP-ENV:Body></SOAP-ENV:Envelope>";

 

However, sometimes you will have the entire structure in your Liquid XML generated Object Model, where the above XML message part is represented as an ‘Any’ element within the XSD. In the scenario, you will need to use the ‘Element’ object within your code.

E.g.  This C# example shows inserting an XML message part as a generic Element object:

Envelope env = new Envelope ();

Element elm = new Element();

elm.FromXml(liquidObj.ToXml());

env.AnyElement.Add(elm);

env.ToXml();


Add Feedback