I have this XML:
<rootnode>
Some text <node1>A Name</node1> some more text <node2>A value</node2>
</rootnode>
Whereas the contents is optional, there can be text in front, between or at the end, and node1
and node2
do not need to be present.
I'd like to serialize this XML to the following C# class:
public class RootNode
{
public String[] Text;
public Node1Type Node1;
public Node2Type Node2;
}
Node1
and Node2
can be more complex elements. The Text
member should contain the mixed in text-parts.
I've tried using this annotated class:
[XmlRoot( ElementName = "rootnode" )]
public class RootNode
{
[XmlText]
public String Text;
[XmlElement( ElementName = "node1" )]
public Node1Type Node1;
[XmlElement( ElementName = "node2" )]
public Node2Type Node2;
}
However it only captures the text at the beginning.
I serialize using this:
public static T ParseXml<T>( String value ) where T : class {
var xmlSerializer = new XmlSerializer( typeof( T ) );
using( var textReader = new StringReader( value ) )
return (T)xmlSerializer.Deserialize( textReader );
}
How do I capture the whole text?