Can .NET XmlSerializer class deserialize InnerXml as a string?

.net, serialization, xml

Solution

You can always implement `IXmlSerializable` and do whatever you fancy through `XmlReader`.

Problem

I have a very specific deserialization need, see example below: say I have following class: ` [Serializable] public class Person { public string Name { get; set; } public string PersonXml { get; set; } } ` and following XML ``` <Person> <Name>John</Name> <PersonXml><someXmlFragment>text</someXmlFragment></PersonXml> </Person> ``` What I want is the XmlSerializer class to deserialize InnerXml of the <PersonXml> element to the PersonXml property as a string. I'm wondering if it can be done. NOTE: I know I can encode the content of <PersonXml> escaping illegal XML chars, but I would prefer to leave the inner XML more human friendly (not containing &lt; and other entities that will only cofuse my end user)

Original source