【发布时间】:2016-01-23 02:21:29
【问题描述】:
使用 DataContractSerializer 序列化具有多个 IDictionary 成员的对象。序列化数据添加了一堆命名空间,例如 d2p1、d3p1 等。我正在尝试使用 xsl 来转换序列化数据以供读取,但是当这些命名空间看起来时,我不知道如何创建 xsl 规则就像它们是动态生成的一样。
是否有使用 xsl 处理此问题的最佳实践方法,或者是否有无需这些额外名称空间的方式对其进行序列化?
序列化数据看起来像这样
<Attributes xmlns:d2p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<d2p1:KeyValueOfstringanyType>
<d2p1:Key>name</d2p1:Key>
<d2p1:Value xmlns:d4p1="http://www.w3.org/2001/XMLSchema" i:type="d4p1:string">Test object</d2p1:Value>
</d2p1:KeyValueOfstringanyType>
<d2p1:KeyValueOfstringanyType>
<d2p1:Key>x</d2p1:Key>
<d2p1:Value xmlns:d4p1="http://www.w3.org/2001/XMLSchema" i:type="d4p1:double">0.5</d2p1:Value>
</d2p1:KeyValueOfstringanyType>
<d2p1:KeyValueOfstringanyType>
<d2p1:Key>y</d2p1:Key>
<d2p1:Value xmlns:d4p1="http://www.w3.org/2001/XMLSchema" i:type="d4p1:double">1.25</d2p1:Value>
</d2p1:KeyValueOfstringanyType>
<d2p1:KeyValueOfstringanyType>
<d2p1:Key>age</d2p1:Key>
<d2p1:Value xmlns:d4p1="http://www.w3.org/2001/XMLSchema" i:type="d4p1:int">4</d2p1:Value>
</d2p1:KeyValueOfstringanyType>
<d2p1:KeyValueOfstringanyType>
<d2p1:Key>list-of-strings</d2p1:Key>
<d2p1:Value>
<d2p1:string>one string</d2p1:string>
<d2p1:string>two string</d2p1:string>
<d2p1:string>last string</d2p1:string>
</d2p1:Value>
</d2p1:KeyValueOfstringanyType>
【问题讨论】:
-
如果我错了,请纠正我,但看起来这些名称空间是在它不知道名称空间时生成的。在这种情况下,它是字典。如果我定义自己的类来实现具有数据协定的 IDictionary,那么它会知道命名空间吗?
-
似乎只有命名空间 prefixes 是动态的,命名空间 URIs 看起来稳定而有规律。您不需要知道使用 XSLT 处理 XML 文档的前缀。如果您在此处要求 XSLT 解决方案:请明确输入 XML 文档是什么,显示您期望的 XML 输出并解释转换规则。不要假设了解 XSLT 的人也了解 C# 和 WCF。求助:stackoverflow.com/help/mcve.
标签: c# xml wcf xslt serialization