Trying to insert CDATA in to an object to be serialized to XML
I have a slew of classes that I generated with xsd.exe for a restful web service I'm trying to consume. The primary request class has a number of other classes as properties, but one of them is expected to be serialized and inserted in a CDATA tag. But I can't for the life of me figure out how to do this effeciently... The best I've come up with is a function that looks like this:
Code:
Function getCData(ByVal req as DetailsRequest)
Dim dummy as New XMLDocument
Dim s as New XmlSerializer(GetType(DetailsRequest))
Dim w as New StringWriter
s.Serialize(w, req)
dummy.Load(w.ToString()) 'see notes below
Dim nodes as XmlNodeList = dummy.GetElementsByTagName("DetailsRequest")
Dim cdataContent as String = "<![CDATA[" & yada yada & "]]>"
return cdataContent
End Function
There HAS to be a better way to do this. What am I missing? (I have about 8 different classes I'd have to do this for if there's no better way!)
The xml being generated by w.ToString() looks like this:
Code:
<?xml version="1.0" encoding="utf-16">
<DetailsRequest xmlns:si = "http://www.w3.org/2001/XMLSchema-instance" xmlns:sd = "http://www.w3.org/2001/XMLSchema">
<TaskId>123456789</TaskId>
</DetailsRequest>
What I need this function to ouput:
Code:
<![CDATA[<DetailsRequest xmlns:si = "http://www.w3.org/2001/XMLSchema-instance" xmlns:sd = "http://www.w3.org/2001/XMLSchema"><TaskId>123456789</TaskId></DetailsRequest>]]>