删除"d1p1" DataContractSerializer XML输出中的名称空间前缀 [英] Remove "d1p1" namespace prefix in DataContractSerializer XML output

查看:118
本文介绍了删除"d1p1" DataContractSerializer XML输出中的名称空间前缀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用DatacontractSerializer将我的domainModel序列化为xml文件. 我得到的输出如下所示.

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>

我想摆脱这些"d1p1"命名空间前缀,只希望</PointDictionary>之类的标签而不是</d1p1:PointsDictionary>. 我试过将DataMember属性放在我的类属性中,但是并不能解决很多问题. 由于最终用户可以编辑输出的XML文件,因此与上述文件相比,我想要一个看上去更干净的XML文件.

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.

首要任务是仅通过代码进行控制,如果不可能,则使用XSLT或任何其他架构.

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

推荐答案

使用空名称空间似乎会删除前缀.使用以下DataContract属性设置您的类:

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

这篇关于删除"d1p1" DataContractSerializer XML输出中的名称空间前缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆