我们在项目中使用合同优先方法来处理WCF服务,使用WSCF Blue将XSD转换为实体,并使用默认序列化。默认的串行器按以下方式序列化数据包
<category xmlns="http://myportal.com/schema/category/v1.0">
<processingDate xmlns="http://myportal.com/schema/common/elements/v1.0">0001-01-01T00:00:00</processingDate>
<key **xmlns="http://myportal.com/schema/common/elements/v1.0"**>f9a8d542-72c8-4465-8d6b-aaeb94a72394</key>
<code>C511746379</code>
<name>category308277327</name>
<description>One Tow</description>
</category>
<region xmlns="http://myportal.com/schema/shared/region/v1.0">
<key **xmlns="http://myportal.com/schema/common/elements/v1.0"**>3</key>
<code>N35</code>
<name>North</name>
<panelCode>N98</panelCode>
</region>
<category xmlns="http://myportal.com/schema/category/v1.0">
<processingDate xmlns="http://myportal.com/schema/common/elements/v1.0">0001-01-01T00:00:00</processingDate>
<key **xmlns="http://myportal.com/schema/common/elements/v1.0"**>00121be8-968f-4dbf-9d5c-d7b81e127a36</key>
<name>Aplha</name>
<code>76542</code>
<createdDate **xmlns="http://myportal.com/schema/common/elements/v1.0"**>2014-03-26T16:36:52.794876</createdDate>
<stream>Online</stream>
</category>
问题以粗体突出显示,为什么默认的序列化程序将整个命名空间放在那里,为什么不能在顶部声明它并使用前缀。整个命名空间扩大了数据包的大小。
类别实体如下所示
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.18058")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://myportal.com/schema/category/v1.0", TypeName="category")]
[System.Xml.Serialization.XmlRootAttribute(Namespace="http://myportal.com/schema/category/v1.0", IsNullable=false, ElementName="category")]
public partial class CategoryType : BaseType
{
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Namespace = "http://myportal.com/schema/common/elements/v1.0", Order = 0, ElementName = "key")]
public string Key
{
get
{
return this.keyField;
}
set
{
this.keyField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Order = 1, ElementName = "code")]
public string Code
{
get
{
return this.codeField;
}
set
{
this.codeField = value;
}
}
如何强制XmlElementAttribute使用前缀而不是完整的命名空间?
谢谢,
阿维
答案 0 :(得分:0)
最后我能够解决这个问题,我的所有DTO(XSD)都是名为BaseType的同一父类的扩展。我不得不添加一个带有XmlNamespaceDeclarations装饰的公共字段,在序列化之前就会查询这个字段。
#region Public Fields
/// <summary>
/// This is considered at the time of serialization for adding namespace prefixes,
/// The namespaces are built in the default constructor, it queries a Constant that in chance fetches the namespace(s) from configuration file
/// </summary>
[XmlNamespaceDeclarations]
public XmlSerializerNamespaces Prefixes;
#endregion
#region Public Constructors
/// <summary>
/// Builds namespace prefixes for serialization
/// </summary>
public BaseType()
{
Prefixes = new XmlSerializerNamespaces();
int index = 1;
Constants.NAMESPACES
.ForEach(tempNamespace =>
Prefixes.Add(Constants.PREFIX_LETTER + index++, tempNamespace)
);
}
#endregion
我希望这个能帮到某个人。
干杯,
阿维