Assuming an XML like this:
<my:Root xmlns:my="http://foo/bar">
<my:FieldBasic>content</my:FieldBasic>
<my:FieldComplex>
<html xml:space="preserve" xmlns="http://www.w3.org/1999/xhtml">
<div><h1>content</h1></div>
</html>
</my:FieldComplex>
<my:Root>
and a class like:
[Serializable]
[XmlType(AnonymousType = true, Namespace = "http://foo/bar")]
[XmlRoot(ElementName = "Root", Namespace = "http://foo/bar", IsNullable = false)]
public class MyRoot
{
public string FieldBasic { get; set; }
public string FieldComplex { get; set; }
}
How do I deserialize <my:FieldComplex>
to a string within FieldComplex
? It fails when it finds the HTML inside. I want to make it give me a string with this content:
<html xml:space="preserve" xmlns="http://www.w3.org/1999/xhtml">
<div><h1>content</h1></div>
</html>
If I declare FieldComplex
as public object FieldComplex
(i.e. xsd:anyType
) it kinda works and I get a XMLNode[]
inside which I can use.
But I need the FieldComplex
to be of type string for the serialization as for serialization the XML will not contain HTML, it will be like:
<my:Root xmlns:my="http://foo/bar">
<my:FieldBasic>content</my:FieldBasic>
<my:FieldComplex>content</my:FieldComplex>
<my:Root>
Declaring FieldComplex
as object will insert these attributes on the <my:FieldComplex>
element:
xmlns:q1="http://www.w3.org/2001/XMLSchema" p3:type="q1:string" xmlns:p3="http://www.w3.org/2001/XMLSchema-instance
and I don't want that. I also don't want to use different classes for serialization and deserialization.
So, is it possible?
To make a long story short, is it possible to have this class:
public class MyRoot
{
public string FieldBasic { get; set; }
public string FielComplex { get; set; }
}
Serialize to this:
<my:Root xmlns:my="http://foo/bar">
<my:FieldBasic>content</my:FieldBasic>
<my:FieldComplex>content</my:FieldComplex>
<my:Root>
and deserialize from this:
<my:Root xmlns:my="http://foo/bar">
<my:FieldBasic>content</my:FieldBasic>
<my:FieldComplex>
<html xml:space="preserve" xmlns="http://www.w3.org/1999/xhtml">
<div><h1>content</h1></div>
</html>
</my:FieldComplex>
<my:Root>
?
P.S. Just to explain "the why?". I have a class witch gets serialized. The serialized XML then travels through multiple nodes in the application and eventually comes back but altered like above. The layers do some XML validation and having extra attributes or elements on input fail the validation and stops the flow. I want to map the return XML to the same class. The content is just strings from it's point of view but of course not the same for serialization/deserialization :(