用的DataContractSerializer多个命名空间 [英] DataContractSerializer with Multiple Namespaces
问题描述
我使用的DataContractSerializer序列化对象到XML。主要对象是与命名空间http://personaltrading.test.com/SecurityHolding并且包含一个属性调用量,与命名空间http://core.test.com一类。当我这个序列化到XML,我得到以下几点:
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>
反正我有可以控制d3p1前缀?难道我做错了什么,或者我应该做点别的?
Is there anyway I can control the d3p1 prefix? Am I doing something wrong or should I be doing something else?
推荐答案
首先,命名空间别名的选择应该没有区别。结构良好的解析器
Firstly, the choice of namespace alias should make no difference to a well-formed parser.
不过,它必须是的DataContractSerializer
?随着的XmlSerializer
,您可以用序列化
,接受一个 XmlSerializerNamespaces $ C的过载$ C>。它可以让你挑选您所使用的名称空间和别名
But; does it have to be DataContractSerializer
? With XmlSerializer
, you can use the overload of Serialize
that accepts a XmlSerializerNamespaces
. This allows you to pick and choose the namespaces and aliases that you use.
最后; 的DataContractSerializer
是的不的目的是充分XML控制;这不是它的目的。如果你想严格的XML控制,的XmlSerializer
是一个更好的选择,即使是较旧的(并有其自身的一些细微之处/弱点)。
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).
完整的示例:
using System;
using System.Xml.Serialization;
public class Amount
{
public const string CoreNamespace = "http://core.test.com/";
[XmlElement("Amount", Namespace=CoreNamespace)]
public decimal Value { get; set; }
[XmlElement("CurrencyCode", Namespace = CoreNamespace)]
public string Currency { get; set; }
}
[XmlType("SecurityHolding", Namespace = SecurityHolding.TradingNamespace)]
public class SecurityHolding
{
public const string TradingNamespace = "http://personaltrading.test.com/";
[XmlElement("Amount", Namespace = Amount.CoreNamespace)]
public Amount Amount { get; set; }
public int BrokerageId { get; set; }
public string BrokerageName { get; set; }
public int RecordId { get; set; }
}
static class Program
{
static void Main()
{
var data = new[] {
new SecurityHolding {
Amount = new Amount {
Value = 1.05M,
Currency = "USD"
},
BrokerageId = 0,
BrokerageName = null,
RecordId = 3681
}
};
var ser = new XmlSerializer(data.GetType(),
new XmlRootAttribute("ArrayOfSecurityHolding") { Namespace = SecurityHolding.TradingNamespace});
var ns = new XmlSerializerNamespaces();
ns.Add("foo", Amount.CoreNamespace);
ser.Serialize(Console.Out, data, ns);
}
}
输出:
Output:
<ArrayOfSecurityHolding xmlns:foo="http://core.test.com/" xmlns="http://personaltrading.test.com/">
<SecurityHolding>
<foo:Amount>
<foo:Amount>1.05</foo:Amount>
<foo:CurrencyCode>USD</foo:CurrencyCode>
</foo:Amount>
<BrokerageId>0</BrokerageId>
<RecordId>3681</RecordId>
</SecurityHolding>
</ArrayOfSecurityHolding>
这篇关于用的DataContractSerializer多个命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!