I have this XML:
<Record>
<ID>123</ID>
<Question>Question goes here...</Question>
<Answer>Answer goes here...</Answer>
<ExtentedAnswer><?xml version="1.0" encoding="utf-16"?><ExtendedResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" DataType="12" xmlns="http://schemas.example.com/"><ComplexText><CityStateZip><City>Clifton</City><State>VA</State><Zip>20124</Zip><Country>US</Country></CityStateZip></ComplexText></ExtendedResponse></ExtentedAnswer>
</Record>
Deserialize works fine, but I get ExtentedAnswer as String. Is it possible to Deserialize XML inside ExtentedAnswer in one go. I know I can Deserialize the ExtentedAnswer separately.
We have tackled this issue at our workplace. In our specific case, the problem was to convert the text with XSLT, which was an utter pain, but is indeed possible. In code it’s less complicated, as suggested by other posters, just substitute the escape codes for
<and>and parse it with a normal parser.However it appears that you want a single-step solution. I doubt very much that the standard XML Serializer will be able to do this, since it has no knowledge of which elements contain escaped xml content. The escaped text is specifically to stop xml parsers from detecting it, so a hand-rolled solution is probably your only option. Your class should implement
IXmlSerializableand perform the text conversion itself.Finally, I would strongly recommend that if you have any control over the process that is creating this data in the first place that you don’t store escaped xml and find another solution. It is a nuisance to work with and I can’t think of any situation where it would be genuinely necessary.