Deserialize XML where XML may contain XML

N

nick_nw

Hi,

Given the following XML (cut down version for explanation, actual doc
much larger).

<?xml version="1.0" encoding="utf-8" ?>
<Stuff>Any string can go in here. It could even be well or badly
formed XML.</Stuff>

When I try to deserialize this document (all I want is the string
content of <Stuff> I get an error if <Stuff> contains XML.

How can instruct the XmlSerializer to ignore the contents of the
<Stuff> element? The only thing I can guarantee is that <Stuff> will
never contain a "</Stuff>" or "<Stuff>" string, but it might contain a
"/>" string.

Am I trying to do the impossible?

Is there a better way to achieve what I want to achieve?

Thanks in advance,

Nick
 
T

Tom Spink

nick_nw said:
Hi,

Given the following XML (cut down version for explanation, actual doc
much larger).

<?xml version="1.0" encoding="utf-8" ?>
<Stuff>Any string can go in here. It could even be well or badly
formed XML.</Stuff>

When I try to deserialize this document (all I want is the string
content of <Stuff> I get an error if <Stuff> contains XML.

How can instruct the XmlSerializer to ignore the contents of the
<Stuff> element? The only thing I can guarantee is that <Stuff> will
never contain a "</Stuff>" or "<Stuff>" string, but it might contain a
"/>" string.

Am I trying to do the impossible?

Is there a better way to achieve what I want to achieve?

Thanks in advance,

Nick

Hi Nick,
Am I trying to do the impossible?

Quite possibly... because even though in a flash, you or I can tell when XML
data appears inside an element (based on our personal perceptions, due to
identation, structure, spelling, context, etc) an XML parser is not that
clever. If the parser reads an element, it doesn't know that it's now
about to read some rubbish, including XML characters like the chevrons,
followed by an end-element.

I believe the answer lies in slightly altering the format of your XML file:

<?xml version="1.0" encoding="utf-8" ?>
<Stuff><![CDATA[Now put your <XML> stuff that may be <BADLY> formed
here]]></Stuff>

The data that may contain malformed XML is wrapped in a
<![CDATA[...]]>
block, where ... is your data.

The following URL may help you:

http://www.w3schools.com/xml/xml_cdata.asp
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top