1

Using c# and visual studio 2015

I'm working with an external webservice and I have build a service reference using a delivered WSDL. That is all working, and I can call the service, but I get a error because the request is not in correct format...

Part of the service definition is this:

<xsd:complexType name="GCTPLookupRequestType">
    <xsd:sequence>    
    <xsd:element name="gctpMessage" type="xsd:string" minOccurs="1" maxOccurs="1" />
    </xsd:sequence>  
  </xsd:complexType>

The gctpMessage is a string element but is expected to contain a CDATA section like this:

<![CDATA[
<Gctp v="1.0">
<System r="CprSoeg">
<Service r="STAM+">
<CprServiceHeader r="STAM+">
<Key>
<Field r="PNR" v="0000000000"/>
</Key>
</CprServiceHeader>
</Service>
</System>
</Gctp>
]]>

If I append this as a string as expected to the gctpMessage property all seems fine, but when I inspect the request using Fiddler I se that it is all wrong:

<gctpMessage>
&lt;![CDATA[&lt;Gctp v="1.0"&gt;&lt;System r="CprSoeg"&gt;&lt;
Service r="STAM+"&gt;&lt;CprServiceHeader r="STAM+"&gt;&lt;Key&gt;&lt;
Field r="PNR" v="0000000000"/&gt;&lt;/Key&gt;&lt;/CprServiceHeader&gt;&lt;/
Service&gt;&lt;/System&gt;&lt;/Gctp&gt;]]&gt;
</gctpMessage>

I know this is caused by the XML serializer enterpreting this as a string and therefore escapes the tags.

But how do I get around this? The WSDL has defined it as a string and I really want to use the service reference but I do not know how to handle this... Changing the service is not an option.

Any suggestion would be appriciated :)

Beaker
  • 227
  • 2
  • 14
  • 2
    Does this help? [How do you serialize a string as CDATA using XmlSerializer?](https://stackoverflow.com/q/1379888). If not can you share your code and how you are initializing the `gctpMessage` property? – dbc Dec 07 '17 at 19:07
  • Seems like a Way around it. – Beaker Dec 07 '17 at 20:04

1 Answers1

2

Just perfect dbc, it did the trick :)

Thanks for that..

  [System.Xml.Serialization.XmlIgnore]
        public string gctpMessage {
            get {
                return this.gctpMessageField;
            }
            set {
                this.gctpMessageField = value;
                this.RaisePropertyChanged("gctpMessage");
            }
        }

        [System.Xml.Serialization.XmlElementAttribute(Order = 1, ElementName = "gctpMessage")]
        public XmlNode[] CDataContent
        {
            get
            {
                var dummy = new XmlDocument();
                return new XmlNode[] { dummy.CreateCDataSection(gctpMessage) };
            }
            set
            {
                if (value == null)
                {
                    gctpMessage = null;
                    return;
                }

                if (value.Length != 1)
                {
                    throw new InvalidOperationException(
                        String.Format(
                            "Invalid array length {0}", value.Length));
                }

                gctpMessage = value[0].Value;
            }
        }
Beaker
  • 227
  • 2
  • 14