Moony
Moony

Reputation: 97

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?

Upvotes: 1

Views: 638

Answers (3)

Jono
Jono

Reputation: 2064

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.

Upvotes: 0

Neil
Neil

Reputation: 7437

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>

Upvotes: 2

ladenedge
ladenedge

Reputation: 13439

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>

Upvotes: 3

Related Questions