locked
Refactor the Properties in latest C# format RRS feed

  • Question

  • User-1256377279 posted

    Hi Guys,

    Below is my class with properties defined with XMl attributes and XML elements, It works fine but i want to refactor the property in much better way. 

    IAMSAccessConditionCV.cs

    using System;
    using System.Xml.Serialization;
    
    namespace IamsXOBLib.Models
    {
        public class IAMSAccessConditionCV
        {
    
            public IAMSAccessConditionCV() { }
    
            #region Instance Attributes
    
            /// <summary>
            /// Holds the unique id of the access condition
            /// </summary>
            protected byte m_accessConditionId;
    
            /// <summary>
            /// Holds the Access Condition description
            /// 
            /// </summary>
            protected string m_accessCondition;
    
            #endregion
    
    
            #region Properties
    
            /// <summary>
            /// Propery to access the access condition id
            /// </summary>
            [XmlAttribute]
            public short AccessConditionId
            {
                get
                {
                    return m_accessConditionId;
                }
                set
                {
                    m_accessConditionId = (byte)value;
                }
            }
    
    
            /// <summary>
            /// Propery to access the Access Condition
            /// </summary>
            [XmlText]
            public string AccessCondition
            {
                get
                {
                    return m_accessCondition;
                }
                set
                {
                    m_accessCondition = value;
                }
            }
    
            #endregion
        }
    }
    

    ArchiveDescription.cs

    using System;
    using System.Xml.Serialization;
    
    namespace IamsXOBLib.Models
    {
        /// <summary>
        /// XOB entities
        /// </summary>
        public class ArchiveDescription
        {
            
    
            [XmlElement(ElementName = "AccessCondition")]
            public IAMSAccessConditionCV IAMSAccessConditionCV
            {
                get
                {
                    return m_accessConditions;
                }
                set
                {
                    m_accessConditions = value;
                }
            }
            private IAMSAccessConditionCV m_accessConditions = null;
    
    
    
    
        }
    }
    

    XobCreation Class

    public string Create(List<IamsLoad.IamsLoadMandat> importlistmandat)
            {
    
                XmlSerializer serializer =  new XmlSerializer(typeof(ArchiveDescription));
                StringBuilder sb = new StringBuilder();
    
                var mandat = importlistmandat.FirstOrDefault(); ;
    
                ArchiveDescription ADItem = new ArchiveDescription();
    
                IAMSAccessConditionCV AC = new IAMSAccessConditionCV();
    
                AC.AccessCondition = mandat.AccessCondition;
                AC.AccessConditionId = 1;
    
                ADItem.IAMSAccessConditionCV = AC;
    
    
              
                string XMLString = getXmlString(ADItem);
                
                return XMLString;                       
    
            }
    
    
            public string getXmlString(ArchiveDescription AD)
            {
                StringWriter l_StringWriter = new StringWriter();
                XmlTextWriter l_TextWriter = new XmlTextWriter(l_StringWriter);
                XmlDocument l_Doc = getXmlDocument(AD);
    
                l_Doc.WriteTo(l_TextWriter);
    
                return l_StringWriter.ToString();
    
            }
    
            public virtual XmlDocument getXmlDocument(ArchiveDescription AD)
            {
                MemoryStream l_Stream = new MemoryStream();
                XmlDocument l_Doc = new XmlDocument();
                XmlSerializer l_Serializer = new  XmlSerializer(typeof(ArchiveDescription));
                XmlSerializerNamespaces l_SerializerNamespaces
                                              = new XmlSerializerNamespaces();
    
                // Remove the default namespaces.
                l_SerializerNamespaces.Add("", "");
    
                l_Serializer.Serialize(l_Stream, AD, l_SerializerNamespaces);
                l_Stream.Position = 0;
                l_Doc.Load(l_Stream);
                l_Stream.Close();
    
                return l_Doc;
    
               
    
            }

    Many Thanks,

    Shabbir

    Monday, February 11, 2019 2:07 PM

Answers

All replies