I'm troubleshooting some old existing .Net 4.6.1 code that is XML serializing this class:
public class Orders
{
private int _pagenumber = 0;
[XmlAttribute]
public int pages
{
get { return _pagenumber; }
set { _pagenumber = value; }
}
[XmlText]
public string OrdersXml { get; set; }
}
The OrdersXml
string contains a block of already-XML-serialized Order
objects (i.e. XML text like: "<Order><OrderId>1</OrderId>...</Order><Order>...</Order>...
"). (They are being XML serialized elsewhere for a variety of reasons and this is not subject to redesign.)
The intent is to include that block of XML verbatim in the serialization of this Orders
object - in other words, as if string OrdersXml
was instead an Orders[] OrdersXML
being serialized as part of the Orders
object, ending up like: <Orders pages="6"><Order><OrderID>123456</OrderID>...</Order>...</Orders>
But that's not happening. The XML in the OrdersXml
property is being serialized as XML-escaped plain text, and it's coming out "<Orders pages="6"><Order><OrderID>2</OrderID>...
" - the code is doing post-serialization cleanup to reverse that, and it's coming out useably correct in most cases. I'd rather it serialize correctly in the first place...
I've tried using [XmlText(typeof(string))]
instead but that didn't help.
Is the XmlSerializer ignoring the [XmlText]
attribute on OrdersXml
, or is that not what [XmlText]
is intended to do?
What is the "correct" best-practice way to composite XML like this?