16

I'm using DatacontractSerializer to serialize my domainModel into a xml file. I'm getting output like below.

<z:anyType xmlns:i="http://www.w3.org/2001/XMLSchema-instance" z:Id="1" xmlns:d1p1="DCSerialization_IGITApproach" i:type="d1p1:X" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/">
  <d1p1:Name z:Id="2">Ankit</d1p1:Name>
  <d1p1:PointsDictionary xmlns:d2p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays" z:Id="3" z:Size="1">
    <d2p1:KeyValueOfstringPointsArrayq9VX7VJJ>
      <d2p1:Key z:Id="4">key1</d2p1:Key>
      <d2p1:Value xmlns:d4p1="http://schemas.datacontract.org/2004/07/SerializationApproach" z:Id="5">
        <d4p1:points z:Id="6" z:Size="2">
          <d2p1:double>45.5</d2p1:double>
          <d2p1:double>546.45</d2p1:double>
        </d4p1:points>
      </d2p1:Value>
    </d2p1:KeyValueOfstringPointsArrayq9VX7VJJ>
  </d1p1:PointsDictionary>
</z:anyType>

And I want to get rid of these "d1p1" namespace prefixes and just want </PointDictionary> like tag instead of </d1p1:PointsDictionary>. I've tried putting DataMember attribute there on my class properties but it doesn't solve much of the problem. As the output XML file can be edited by the end user so I want a cleaner looking XML file as compared to the above one.

First priority is to control it through code only and if not possible then go for XSLT or any other schema.

Nalaka526
  • 11,278
  • 21
  • 82
  • 116
Bobby
  • 169
  • 1
  • 2
  • 9
  • Welcome to StackOverflow: if you post code, XML or data samples, **please** highlight those lines in the text editor and click on the "code samples" button ( `{ }` ) on the editor toolbar to nicely format and syntax highlight it! – marc_s Feb 22 '12 at 10:24

3 Answers3

6

Using an empty namespace seems to remove the prefix. Setup your class with the following DataContract attribute:

[DataContract(Namespace="")]
public class MyClass
{ ... }

Then be sure to set the namespace to an empty string when (de)serializing:

DataContractSerializer deserializer = new DataContractSerializer(typeof(MyClass), typeof(MyClass).Name, "");
Dru
  • 61
  • 1
  • 2
4

It looks like DataContractSerializer doesn't give much control over prefixes. The answer to XML Serialization and namespace prefixes suggests using XmlSerializer if you want to control the namespace prefix.

Your question wasn't clear as to whether you wanted to entirely remove the namespace prefixes for your domain model. Your sample above has several namespace prefixes: d1p1, d2p1, d4p1. Changing namespace for XML file in XSL Translation provides some guidance on prefix renaming using XSLT.

Community
  • 1
  • 1
Matt Schouten
  • 141
  • 2
  • 3
  • 5
    XmlSerializer suffers from two limitations compared to DataContractSerializer: it won't handle interfaces, and it forces every class to have a parameterless constructor. I believe its a better long term solution to use DataContractSerializer and find some other method of dealing with the namespace issue. – Contango Feb 24 '14 at 09:07
  • @Contango, I agree in principle... except that `DataContractSerializer` *does* (in my experience) require a parameterless constructor. I get a runtime error if I try to instantiate it on a type that doesn't have one. – harpo Jan 17 '15 at 01:17
  • @harpo DataContractSerializer definitely does not require an empty constructor. You're doing something incorrectly. If you'd like to post your code in a new question I'd be happy to review it. – Dan Bechard May 01 '15 at 19:25
  • @Dan, I'm sure you're right, and I'll stand corrected. For the project in question, I ended up going with `XmlSerializer`, trading (at least potential) immutability for finer control over serialization. – harpo May 04 '15 at 19:46
  • @harpo I'm still curious how you were attempting to utilize DataContractSerializer, especially if you think XmlSerializer offers finer control. In fact, that's the very reason I've started moving away from XmlSerializer myself. You can gain many of the advantages of XML without the limitations of XmlSerializer by using DataContractSerializer in combination with an XmlWriter. Perhaps, when you have some spare time, you might be interested in investigating that approach. – Dan Bechard May 06 '15 at 15:41
  • another fail with XmlSerializer is that it adds items to list rather than reinstating list with new elements on deserialization. – Jakub Pawlinski Oct 15 '15 at 10:20
0

You should be able to get rid of those prefixes by just making sure that the classes that you are trying to serialize to XML are within the same Namespace. For example I had two classes ApplicationListResponse and Application. Previously the namespaces were Models.Responses and Models.Responses.Application. I changed both of the Namespaces to be "Models" and that got rid of the prefix in the XML output.

R007
  • 378
  • 4
  • 11