I need to serialize my Invoice model to Xml in 2 different ways.
First serialization should have "FirsName" as root element.
Second "SecondName".
There is different ways to implement, but i don't know how to implement them.
Either to avoid root element and add it manualy, or somehow dynamically preset it.
Here is my model:
public class InvoiceInfo : IXmlSerializable
{
public int InvoiceId { get; set; }
}
Depending on condition I want to serialize it like:
<firstRoot>
<invoiceId value="123" />
</firstRoot>
or
<secondRoot>
<invoiceId value="123" />
</secondRoot>
Maybe it can solved by adjusting XmlWriter.Settings ?
I've found this approuch, but it seems ugly. Because it's kinda post processing...
var duplicate = XDocument.Parse(xmlDocument.InnerXml);
duplicate.Root.Name = "newRootName";
var res = duplicate.ToString();
You can dynamically add
XmlRootAttribute
:Your model
See Dynamically Generated Assemblies - you must cache the assemblies.