Remove “d1p1” namespace prefix in DataContractSerializer XML output

会有一股神秘感。 提交于 2019-12-04 01:23:30

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, "");
Matt Schouten

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!