views:

163

answers:

3
+1  Q: 

c# xml deserialize

I have xml wherein i have xml within it again, like:

<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<Tag>
<Value1> </Value1>
<Value2><?xml version=\"1.0\" encoding=\"UTF-8\"?>... </Value2>
</Tag>

Deserializing doesnt work on this string in c#. I construct this string in java and send it to a c# app. how can i get around this?

+2  A: 

The XML you show isn't well-formed. Strings need to be encoded before they are placed in the XML output stream. Your XML should look like this:

<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<Tag>
  <Value1></Value1>
  <Value2>&lt;?xml version=&quot;1.0&quot; ... </Value2>
</Tag>
ladenedge
+1  A: 

One approach would be to wrap the <?xml version=\"1.0\" encoding=\"UTF-8\"?>... as a CData section:

<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<Tag>
<Value1> </Value1>
<Value2><![CDATA[<?xml version=\"1.0\" encoding=\"UTF-8\"?>... </Value2>]]>
</Tag>
Neil Whitaker
thanks works nicely
Moony
A: 

The horrible answer you didn't want to hear is that XML is more than just a string. For encoding and other reasons, you can't reliably substitute string fragments into other fragments and expect well-formed documents. If you're not using a proper XML library (which you should, by the way, they're in all the major frameworks) you can still hack XML fragment strings together by making sure their encodings are actually the same, and by removing the <?xml version="1.0" encoding="UTF-8"?> from the start of any of the fragments.

Jono
Assuming you actually wanted to recursively store one document within another, a CDATA section will work for the outer document but you cannot recursively store CDATA sections in the inner document too.
Jono