我正在构建一个自托管的WCF服务。我正在构建一个非常灵活的数据传输的特殊数据结构。到目前为止,我测试了我的结构是否可以使用DataContractSerializer进行序列化。这工作得很好,我感到高兴,但也有一些是讨厌我:为DataContractSerializer预定义XML名称空间
在我的XML输出几十重新定义的xmlns属性,如:
xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays"
xmlns:b="http://www.w3.org/2001/XMLSchema"
这应该在根元素可以更好地定义一次所以可以简单地优化字节。有没有办法将自定义命名空间信息添加到根元素?
这是一个更大的例子来说明我的意思:
<DataObject xmlns="http://schemas.datacontract.org/2004/07/Test"
xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Data xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<a:KeyValueOfstringanyType>
<a:Key>ID</a:Key>
<a:Value i:type="b:int" xmlns:b="http://www.w3.org/2001/XMLSchema">1</a:Value>
</a:KeyValueOfstringanyType>
<a:KeyValueOfstringanyType>
<a:Key>Value</a:Key>
<a:Value i:type="b:int" xmlns:b="http://www.w3.org/2001/XMLSchema">42</a:Value>
</a:KeyValueOfstringanyType>
</Data>
<Data xmlns:a="...">...</Data>
<Data xmlns:a="...">...</Data>
<Data xmlns:a="...">...</Data>
</DataObject>
我想是这样的:
<DataObject xmlns="http://schemas.datacontract.org/2004/07/Test"
xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays"
xmlns:b="http://www.w3.org/2001/XMLSchema">
<Data>
<a:KeyValueOfstringanyType>
<a:Key>ID</a:Key>
<a:Value i:type="b:int">1</a:Value>
</a:KeyValueOfstringanyType>
<a:KeyValueOfstringanyType>
<a:Key>Value</a:Key>
<a:Value i:type="b:int">42</a:Value>
</a:KeyValueOfstringanyType>
</Data>
<Data>...</Data>
<Data>...</Data>
<Data>...</Data>
</DataObject>
(HTTP [如何序列化对象到XML没有得到的xmlns =“...”?]:/ /stackoverflow.com/questions/258960/how-to-serialize-an-object-to-xml-without-getting-xmlns)< - 这包含你想要的 –
@MarkusJarderot没有不是真的。这似乎是一种摆脱通用名称空间的方式。 WCF合同需要(到目前为止我知道)!我只是想避免有严重的重新定义。 – rekire