Click here to Skip to main content
15,303,151 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I have a serious problem with sending xml CDATA section through web service.

I have method witch sending xml to SOAP in proper section:

HTML
HTTP/1.1 200 OK
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length


<soap12:envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:body>
    <stockcompleteresponse xmlns="http://xxx.com/">
      <stockcompleteresult>xml</stockcompleteresult>
    </stockcompleteresponse>
  </soap12:body>
</soap12:envelope>


In line 7 of this code should be placed XML with CDATA section only. I have built this XML with code on server method (vb.net):

VB
Dim builder As New StringBuilder
       
        builder.Append("<stockinfo>")
        For Each dr As DataRow In ds.Tables(0).Rows
            builder.Append("<product>")
            builder.Append("<productid>" & dr.Item("PRODUCTID") & "</productid>")
            builder.Append("<stock>" & dr.Item("STOCK") & "</stock>")
            builder.Append("</product>")
        Next
        builder.Append("</stockinfo>")

        value.LoadXml("<stock></stock>")

        Dim CData As XmlCDataSection
        CData = value.CreateCDataSection(builder.ToString())

        Dim root As XmlElement = value.DocumentElement
        root.AppendChild(CData)


"value" is XMLDocument datatype and whole function returns XMLDocument.

I know that this method of building xml is good, because i get proper xml when calling webservice, but my client see error message. The reason is simple: root in returned xml.
So my question is: how to send only CDATA section in XML without root? Is it possible?
I've tried with string datatype, but then I see error message too.
Posted

1 solution

I think your problem is not a problem of XML programming, but a conceptual one. The thing is: CDATA section is not a part of XML logical structure. From the logical structure perspective, such thing simply does not exist. CDATA is merely an expressive tool used to enter character data containing XML markup characters.

Consider these two XML code samples:

With CDATA:
XML
<rootNode>
<![CDATA[This is some HTML code sample: <p>Paragraph</p>]]>
</rootNode>


Without CDATA:
XML
<rootNode>
This is some HTML code sample: &lt;p&gt;Paragraph&lt;/p&gt;
</rootNode>


When these fragments are parsed by a standard XML parser, for example, into DOM structure, no difference between them can be found. From the standpoint of logical structure, they are the same. First sample uses CDATA, second one — XML character entities. Both methods are used to "escape" XML markup characters '<' and '>; CDATA is simply a shorter way to write the same thing. Both have nothing to do with logical structure. They simply don't have to be "sent".

—SA
   
v4
Comments
szataniel 26-Sep-11 14:27pm
   
You are right... I have to change webmethod and some things in webservice. Thanks for Your support... again. :)
Sergey Alexandrovich Kryukov 26-Sep-11 17:07pm
   
I'm glad if this helps.
Good luck, call again. :-)
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900