سؤال

We are using contract first approach for WCF services in our project, XSD(s) are converted to entities using WSCF Blue and default serialization is used. The default serializer serializes the packets in following way

<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> 

The problem is highlighted in bold, why the default serializer puts the whole namespace there, why can't it declare it at the top and use prefix. The whole namespace inflates size of packet.

The category entity looks like following

[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;
        }
    }

How do I enforce the XmlElementAttribute to use prefix instead of complete namespace?

Thanks,

Avi

هل كانت مفيدة؟

المحلول

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

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top