Mike Flynn
Mike Flynn

Reputation: 24325

Namespace Not Being Removed From XML Root with DataContractSerializer

Does anyone know why the namespace is still being added to the root during datacontractserialization?

XML:

<Response xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
  <Event>
    <Address>
      .
      .
      .
</Response>

Code:

[DataContract(Name = "Response", Namespace = "")]
public class ApiEventResponse
{
    [DataMember(EmitDefaultValue = false)]
    public ApiEvent Event { get; set; }
}

var serializer = new DataContractSerializer(type, "Response", "");

return Task.Factory.StartNew(() =>
  {
      using (var xmlw = new XmlTextWriter(writeStream, Encoding))
      {
          xmlw.Formatting = Formatting.Indented;
          serializer.WriteObject(xmlw, value);
      }
  });

Upvotes: 1

Views: 645

Answers (1)

dthorpe
dthorpe

Reputation: 36092

The XMLSchema-instance namespace appears to be emitted regardless of what data contracts or actual namespaces are used. The XMLSchema-instance namespace prefix isn't actually used in your example, it's just declared.

The serializer is probably just emitting that namespace always in case it later discovers a situation where it needs to reference XMLSchema while serializing your data.

Upvotes: 2

Related Questions