删除“d1p1” DataContractSerializer XML 输出中的命名空间前缀

发布于 2025-01-08 07:37:01 字数 1223 浏览 5 评论 0原文

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

<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”命名空间前缀,只想要 像标记而不是 。 我尝试将 DataMember 属性放在我的类属性上,但它并不能解决很多问题。 由于最终用户可以编辑输出 XML 文件,因此我想要一个与上面的文件相比看起来更干净的 XML 文件。

第一要务是仅通过代码来控制它,如果不可能,则使用 XSLT 或任何其他模式。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

深居我梦 2025-01-15 07:37:01

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

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

然后确保在(反)序列化时将命名空间设置为空字符串:

DataContractSerializer deserializer = new DataContractSerializer(typeof(MyClass), typeof(MyClass).Name, "");

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, "");
暖阳 2025-01-15 07:37:01

看起来 DataContractSerializer 并没有对前缀进行太多控制。 XML 序列化和命名空间前缀 的答案建议使用 XmlSerializer如果你想控制命名空间前缀。

您的问题不清楚是否要完全删除域模型的命名空间前缀。上面的示例有几个命名空间前缀:d1p1、d2p1、d4p1。 在 XSL 翻译中更改 XML 文件的命名空间提供了一些关于使用 XSLT 进行前缀重命名。

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.

不忘初心 2025-01-15 07:37:01

只需确保尝试序列化为 XML 的类位于同一命名空间内,您就应该能够摆脱这些前缀。例如,我有两个类 ApplicationListResponse 和 Application。以前的命名空间是 Models.Responses 和 Models.Responses.Application。我将两个命名空间都更改为“Models”,并且去掉了 XML 输出中的前缀。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文