I'm looking for a way with C# which I can serialize a class into XML and add a namespace, but define the prefix which that namespace will use.
Ultimately I'm trying to generate the following XML:
<myNamespace:Node xmlns:myNamespace="...">
<childNode>something in here</childNode>
</myNamespace:Node>
I know with both the DataContractSerializer
and the XmlSerializer
I can add a namespace, but they seem to generate a prefix internally, with something that I'm not able to control. Am I able to control it with either of these serializers (I can use either of them)?
If I'm not able to control the generation of the namespaces will I need to write my own XML serializer, and if so, what's the best one to write it for?
Best Answer
To control the namespace alias, use
XmlSerializerNamespaces
.If you need to change the namespace at runtime, you can additionally use
XmlAttributeOverrides
.