Possible Duplicate:
How to Include CData using LINQ to XML?
About a month ago I wrote a post about wrapping some data within CDATA tags for a webservice (it's their requirement, not mine). I had done a bunch of troubleshooting and worked to an example that provided some results. (see post Problems serializing a class to XML and including a CDATA section for the code and results )
Now I have realized that the class design was terrible to say the least and have rewritten most of the code I had written previously. As such it has taken some time to get back to the same question I had before.
For the sake of post brevity I have decided to ask a new question and leave the old one as is.
My (revised ) question
Am I able to use XmlDocument to create the elements up to the CDATA section, and then use the CreateCDataSection()
method to create the CDATA section as below?
<UpdateOrderStatus>
<Action>2</Action>
<Value>
<![CDATA[
<Shipment>
<Header>
<SellerID>
...
]]>
</Value>
</UpdateOrderStatus>
I am able to serialize my existing objects into the proper XML fragment, however I am not 100% certain on if this code is correct or if there is a better way:
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.CreateDocumentFragment();
xmlDoc.CreateElement("Shipment");
xmlDoc.CreateElement("Action");
xmlDoc.CreateTextNode("2");
xmlDoc.CreateElement("Value");
xmlDoc.CreateCDataSection(orderToUpdate);
If you were programming this, how would you go about it? I don't necessarily need code, unless the concept can only be shown that way. A class name, or a MSDN reference etc would be fine