Remove “d1p1” namespace prefix in DataContractSerializer XML output

前端 未结 3 789
长情又很酷
长情又很酷 2021-01-04 04:22

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



        
相关标签:
3条回答
  • 2021-01-04 04:58

    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.

    0 讨论(0)
  • 2021-01-04 05:03

    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, "");
    
    0 讨论(0)
  • 2021-01-04 05:14

    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.

    0 讨论(0)
提交回复
热议问题