I have a type that implements IXmlSerializable which I am serializing with DataContractSerializer. How can I control the root element namespace and name when serializing it
This can be done using attributes in one of two ways.
Firstly (and surprisingly) if you apply the [XmlRoot] attribute for the old XmlSerializer
to the type, DataContractSerializer
will use the namespace and name specified therein as the root data contract namespace and name:
[XmlRoot("Person", Namespace = "http://www.MyCompany.com")]
public partial class PersonDTO : IXmlSerializable
{
}
Which generates the following XML:
<Person name="John Doe" xmlns="http://www.MyCompany.com" />
However, this solution only applies to the root element name. If you try to serialize an array or generic list of such objects the unmodified namespace and name are used:
<ArrayOfPersonDTO xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/MyClrNamespace">
<PersonDTO name="John Doe" />
</ArrayOfPersonDTO>
Secondly and more powerfully, the [XmlSchemaProvider] attribute can be used to specify a static method that returns a data contract name, namespace and schema for the type:
[XmlSchemaProvider("GetSchemaMethod")]
public partial class PersonDTO : IXmlSerializable
{
// This is the method named by the XmlSchemaProviderAttribute applied to the type.
public static XmlQualifiedName GetSchemaMethod(XmlSchemaSet xs)
{
// Fill in a plausible schema for the type if necessary.
//
// While DataContractSerializer will not use the returned schema set,
// svcutil.exe will use it to generate schemas. XmlSerializer also
// seems to require it to be initialized to something plausible if you
// are serializing your types with both serializers.
string personSchema = @"<xs:schema xmlns:tns=""http://www.MyCompany.com"" elementFormDefault=""qualified"" targetNamespace=""http://www.MyCompany.com"" xmlns:xs=""http://www.w3.org/2001/XMLSchema"">
<xs:element name=""Person"" nillable=""true"" type=""tns:Person"" />
<xs:complexType name=""Person"">
<xs:attribute name=""name"" type=""xs:string"" />
</xs:complexType>
</xs:schema>";
using (var textReader = new StringReader(personSchema))
using (var schemaSetReader = System.Xml.XmlReader.Create(textReader))
{
xs.Add("http://www.MyCompany.com", schemaSetReader);
}
// Return back the namespace and name to be used for this type.
return new XmlQualifiedName("Person", "http://www.MyCompany.com");
}
}
This has the advantage that not only will the root name and namespace be modified, but also the data contract name used in arrays, generic collections, and other generics will be as well:
<ArrayOfPerson xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.MyCompany.com">
<Person name="John Doe" />
</ArrayOfPerson>
Notes:
DataContractSerializer
uses only the XmlQualifiedName
returned by the schema provider method. However, if you plan to generate an XSD for your type using svcutil.exe
or also serialize your type with XmlSerializer
, you will need to fill in the XmlSchemaSet xs
with something plausible. (And when you do, the XSD generated will reflect the returned schema.)