简体   繁体   English

使用服务参考将CDATA部分作为字符串

[英]CDATA section as string using service reference

Using c# and visual studio 2015 使用C#和Visual Studio 2015

I'm working with an external webservice and I have build a service reference using a delivered WSDL. 我正在使用外部Web服务,并且已经使用提供的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: gctpMessage是一个字符串元素,但是应该包含一个CDATA部分,如下所示:

<![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属性,那么一切似乎都很好,但是当我使用Fiddler检查请求时,我发现这都是错误的:

<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. 我知道这是由XML序列化器将其输入为字符串并因此转义标签引起的。

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. WSDL已将其定义为字符串,我确实想使用服务引用,但我不知道如何处理...更改服务不是一种选择。

Any suggestion would be appriciated :) 任何建议都会被采纳:)

Just perfect dbc, it did the trick :) 只是完美的dbc,它成功了:)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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