具有多个命名空间的 DataContractSerializer
我正在使用 DataContractSerializer 将对象序列化为 XML。主要对象是 SecurityHolding,其命名空间为“http://personaltrading.test.com/”,并包含一个属性名为 Amount 的类,其命名空间为“http://core.test.com”。当我将其序列化为 XML 时,我得到以下信息:
<ArrayOfSecurityHolding xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://personaltrading.test.com/">
<SecurityHolding>
<Amount xmlns:d3p1="http://core.test.com/">
<d3p1:Amount>1.05</d3p1:Amount>
<d3p1:CurrencyCode>USD</d3p1:CurrencyCode>
</Amount>
<BrokerageID>0</BrokerageID>
<BrokerageName i:nil="true" />
<RecordID>3681</RecordID>
</SecurityHolding></ArrayOfSecurityHolding>
我可以控制 d3p1 前缀吗?我是否做错了什么或者我应该做其他事情?
I am using a DataContractSerializer to serialize an object to XML. The main object is SecurityHolding with the namespace "http://personaltrading.test.com/" and contains a property called Amount that's a class with the namespace "http://core.test.com". When I serialize this to XML I get the following:
<ArrayOfSecurityHolding xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://personaltrading.test.com/">
<SecurityHolding>
<Amount xmlns:d3p1="http://core.test.com/">
<d3p1:Amount>1.05</d3p1:Amount>
<d3p1:CurrencyCode>USD</d3p1:CurrencyCode>
</Amount>
<BrokerageID>0</BrokerageID>
<BrokerageName i:nil="true" />
<RecordID>3681</RecordID>
</SecurityHolding></ArrayOfSecurityHolding>
Is there anyway I can control the d3p1 prefix? Am I doing something wrong or should I be doing something else?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我也曾为这个问题苦苦挣扎。我在下面提出的解决方案不是最佳的恕我直言,但它有效。就像上面的 Marc Gravell 一样,我建议使用 XmlSerializer。
诀窍是向您的类添加一个返回 XmlSerializerNamespaces 对象的字段。该字段必须用 XmlNamespaceDeclarations 属性修饰。在类的构造函数中,添加命名空间,如下例所示。在下面的 xml 中,请注意根元素以及 someString 元素的前缀正确。
有关 XmlSerializerNamespace 的详细信息
架构参考
I have struggled with this problem also. The solution I present below is not optimal IMHO but it works. Like Marc Gravell above, I suggest using XmlSerializer.
The trick is to add a field to your class that returns a XmlSerializerNamespaces object. This field must be decorated with a XmlNamespaceDeclarations attribute. In the constructor of your class, add namespaces as shown in the example below. In the xml below note that the root element is prefixed correctly as well as the someString element.
More info on XmlSerializerNamespaces
Schemas reference
添加“http://www.w3.org/2001/XMLSchema”命名空间:
Add "http://www.w3.org/2001/XMLSchema" namespace by:
首先,命名空间别名的选择对于格式良好的解析器应该没有影响。
但;它必须是 DataContractSerializer 吗?通过
XmlSerializer
,您可以使用接受XmlSerializerNamespaces
的Serialize
重载。这允许您选择您使用的名称空间和别名。最终;
DataContractSerializer
不旨在提供完整的 xml 控制;这不是它的目标。如果您想要严格的 xml 控制,XmlSerializer
是更好的选择,即使它较旧(并且有自己的一些细微差别/缺点)。完整示例:
输出:
Firstly, the choice of namespace alias should make no difference to a well-formed parser.
But; does it have to be
DataContractSerializer
? WithXmlSerializer
, you can use the overload ofSerialize
that accepts aXmlSerializerNamespaces
. This allows you to pick and choose the namespaces and aliases that you use.Ultimately;
DataContractSerializer
is not intended to give full xml control; that isn't its aim. If you want strict xml control,XmlSerializer
is a better choice, even if it is older (and has some nuances/foibles of its own).Full example:
Output:
我解决这个问题的方法与 Marc 略有不同,可以在基类中实现。
创建一个新属性来定义您将在数据协定中使用的其他 XML 命名空间。
将该属性添加到您的数据契约中。
然后在您的
Save
方法中,使用反射来获取属性,然后将它们写入文件。I have solved this problem slightly differently to Marc that can be implemented in a base class.
Create a new attribute to define the additional XML namespaces that you will use in your data contract.
Add the attribute to your data contracts.
Then in your
Save
method, use reflection to get the attributes and then write them to the file.