1

我们在项目中对 WCF 服务使用契约优先方法,XSD 使用 WSCF Blue 转换为实体,并使用默认序列化。默认序列化程序以下列方式序列化数据包

<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 使用前缀而不是完整的命名空间?

谢谢,

阿维

4

1 回答 1

0

Finally I was able to resolve this one, all of my DTO (XSD) were an extension of same parent class by the name BaseType. I had to add a public field with XmlNamespaceDeclarations decoration, this field is consulted right before serialization.

    #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

I hope this one helps somebody.

Cheers,

Avi

于 2014-12-17T05:40:47.433 回答