Suppose I have a class:
using System.Xml;
using System.Xml.Serialization;
class Foo {
[XmlElement(ElementName="Bar")]
public string Bar {get; set;}
}
And now I want to serialize it. But. I want to specify different namespaces, On one occasion I want my XML look like this:
<Foo xmlns:v1="http://site1">
<v1:Bar />
</Foo>
And on the other one - like this:
<Foo xmlns:v2="http://site2">
<v2:Bar />
</Foo>
I know I need to specify the namespace in the XmlElement attribute, but this is exactly what I want to avoid. I could, of course, make 2 different classes which are identical except the field attributes, but this feels wrong somehow. Is there any way I could force the XmlSerializer use the namespace I choose at runtime?