How to configure a Custom Datacontract Serializer or XMLSerializer

Posted by user364445 on Stack Overflow See other posts from Stack Overflow or by user364445
Published on 2010-06-11T10:49:53Z Indexed on 2010/06/11 10:52 UTC
Read the original article Hit count: 818

Filed under:

Im haveing some xml that have this structure

<Person Id="*****" Name="*****">
  <AccessControlEntries>
    <AccessControlEntry Id="*****" Name="****"/>
  </AccessControlEntries>
  <AccessControls />
  <IdentityGroups>
    <IdentityGroup Id="****" Name="*****" />
  </IdentityGroups></Person>

and i also have this entities

[DataContract(IsReference = true)]
public abstract class EntityBase
{
    protected bool serializing; 

    [DataMember(Order = 1)]
    [XmlAttribute()]
    public string Id { get; set; }

    [DataMember(Order = 2)]
    [XmlAttribute()]
    public string Name { get; set; }

    [OnDeserializing()]
    public void OnDeserializing(StreamingContext context)
    {
        this.Initialize();
    }

    [OnSerializing()]
    public void OnSerializing(StreamingContext context)
    {
        this.serializing = true;
    }

    [OnSerialized()]
    public void OnSerialized(StreamingContext context)
    {
        this.serializing = false;
    }

    public abstract void Initialize();

    public string ToXml()
    {
        var settings = new System.Xml.XmlWriterSettings();
        settings.Indent = true;
        settings.OmitXmlDeclaration = true;

        var sb = new System.Text.StringBuilder();
        using (var writer = System.Xml.XmlWriter.Create(sb, settings))
        {
            var serializer = new XmlSerializer(this.GetType());
            serializer.Serialize(writer, this);
        }

        return sb.ToString();
    }
}

[DataContract()]
public abstract class Identity : EntityBase
{
    private EntitySet<AccessControlEntry> accessControlEntries;
    private EntitySet<IdentityGroup> identityGroups;

    public Identity()
    {
        Initialize();
    }

    [DataMember(Order = 3, EmitDefaultValue = false)]
    [Association(Name = "AccessControlEntries")]
    public EntitySet<AccessControlEntry> AccessControlEntries
    {
        get
        {
            if ((this.serializing && (this.accessControlEntries==null || this.accessControlEntries.HasLoadedOrAssignedValues == false)))
            {
                return null;
            }
            return accessControlEntries;
        }
        set
        {
            accessControlEntries.Assign(value);
        }
    }

    [DataMember(Order = 4, EmitDefaultValue = false)]
    [Association(Name = "IdentityGroups")]
    public EntitySet<IdentityGroup> IdentityGroups
    {
        get 
        {
            if ((this.serializing && (this.identityGroups == null || this.identityGroups.HasLoadedOrAssignedValues == false)))
            {
                return null;
            }
            return identityGroups; 
        }

        set
        {
            identityGroups.Assign(value);
        }
    }

    private void attach_accessControlEntry(AccessControlEntry entity)
    {
        entity.Identities.Add(this);
    }

    private void dettach_accessControlEntry(AccessControlEntry entity)
    {
        entity.Identities.Remove(this);
    }

    private void attach_IdentityGroup(IdentityGroup entity)
    {
        entity.MemberIdentites.Add(this);
    }

    private void dettach_IdentityGroup(IdentityGroup entity)
    {
        entity.MemberIdentites.Add(this);
    }

    public override void Initialize()
    {
        this.accessControlEntries = new EntitySet<AccessControlEntry>(
            new Action<AccessControlEntry>(this.attach_accessControlEntry),
            new Action<AccessControlEntry>(this.dettach_accessControlEntry));
        this.identityGroups = new EntitySet<IdentityGroup>(
            new Action<IdentityGroup>(this.attach_IdentityGroup),
            new Action<IdentityGroup>(this.dettach_IdentityGroup));
    }
}

[XmlType(TypeName = "AccessControlEntry")]
public class AccessControlEntry : EntityBase, INotifyPropertyChanged
{
    private EntitySet<Service> services;
    private EntitySet<Identity> identities;
    private EntitySet<Permission> permissions;

    public AccessControlEntry()
    {
        services = new EntitySet<Service>(new Action<Service>(attach_Service), new Action<Service>(dettach_Service));
        identities = new EntitySet<Identity>(new Action<Identity>(attach_Identity), new Action<Identity>(dettach_Identity));
        permissions = new EntitySet<Permission>(new Action<Permission>(attach_Permission), new Action<Permission>(dettach_Permission));
    }

    [DataMember(Order = 3, EmitDefaultValue = false)]
    public EntitySet<Permission> Permissions
    {
        get {
            if ((this.serializing && (this.permissions.HasLoadedOrAssignedValues == false)))
            {
                return null;
            }
            return permissions; 
        }
        set
        {
            permissions.Assign(value);
        }
    }

    [DataMember(Order = 4, EmitDefaultValue = false)]
    public EntitySet<Identity> Identities
    {
        get
        {
            if ((this.serializing && (this.identities.HasLoadedOrAssignedValues == false)))
            {
                return null;
            }
            return identities;
        }
        set
        {
            identities.Assign(identities);
        }
    }

    [DataMember(Order = 5, EmitDefaultValue = false)]
    public EntitySet<Service> Services
    {
        get
        {
            if ((this.serializing && (this.services.HasLoadedOrAssignedValues == false)))
            {
                return null;
            }
            return services;
        }
        set
        {
            services.Assign(value);
        }
    }

    private void attach_Permission(Permission entity)
    {
        entity.AccessControlEntires.Add(this);
    }
    private void dettach_Permission(Permission entity)
    {
        entity.AccessControlEntires.Remove(this);
    }

    private void attach_Identity(Identity entity)
    {
        entity.AccessControlEntries.Add(this);
    }
    private void dettach_Identity(Identity entity)
    {
        entity.AccessControlEntries.Remove(this);
    }

    private void attach_Service(Service entity)
    {
        entity.AccessControlEntries.Add(this);
    }
    private void dettach_Service(Service entity)
    {
        entity.AccessControlEntries.Remove(this);
    }

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
            handler(this, new PropertyChangedEventArgs(name));
    }

    #endregion

    public override void Initialize()
    {
        throw new NotImplementedException();
    }
}

[DataContract()]
[XmlType(TypeName = "Person")]
public class Person : Identity
{
    private EntityRef<Login> login;

    [DataMember(Order = 3)]
    [XmlAttribute()]
    public string Nombre { get; set; }

    [DataMember(Order = 4)]
    [XmlAttribute()]
    public string Apellidos { get; set; }

    [DataMember(Order = 5)]
    public Login Login {
        get
        {
            return login.Entity;
        }
        set
        {
            var previousValue = this.login.Entity;
            if (((previousValue != value) || (this.login.HasLoadedOrAssignedValue == false)))
            {
                if ((previousValue != null))
                {
                    this.login.Entity = null;
                    previousValue.Person = null;
                }
                this.login.Entity = value;

                if ((value != null))
                    value.Person = this;
            }
        }
    }

    public override void Initialize()
    {
        base.Initialize();
    }
}

[DataContract()]
[XmlType(TypeName = "Login")]
public class Login : EntityBase
{
    private EntityRef<Person> person;

    [DataMember(Order = 3)]
    public string UserID { get; set; }

    [DataMember(Order = 4)]
    public string Contrasena { get; set; }

    [DataMember(Order = 5)]
    public Domain Dominio
    {
        get;
        set;
    }

    public Person Person
    {
        get 
        {
            return person.Entity;
        }
        set
        {
            var previousValue = this.person.Entity;
            if (((previousValue != value) || (this.person.HasLoadedOrAssignedValue == false)))
            {
                if ((previousValue != null))
                {
                    this.person.Entity = null;
                    previousValue.Login = null;
                }
                this.person.Entity = value;

                if ((value != null))
                    value.Login = this;
            }
        }
    }

    public override void Initialize()
    {
        throw new NotImplementedException();
    }
}

[DataContract()]
[XmlType(TypeName = "IdentityGroup")]
public class IdentityGroup : Identity
{
    private EntitySet<Identity> memberIdentities;

    public IdentityGroup()
    {
        Initialize();
    }

    public override void Initialize()
    {
        this.memberIdentities = new EntitySet<Identity>(new Action<Identity>(this.attach_Identity), new Action<Identity>(this.dettach_Identity));
    }

    [DataMember(Order = 3, EmitDefaultValue = false)]
    [Association(Name = "MemberIdentities")]
    public EntitySet<Identity> MemberIdentites
    {
        get
        {
            if ((this.serializing && (this.memberIdentities.HasLoadedOrAssignedValues == false)))
            {
                return null;
            }
            return memberIdentities;
        }
        set
        {
            memberIdentities.Assign(value);
        }
    }

    private void attach_Identity(Identity entity)
    {
        entity.IdentityGroups.Add(this);
    }

    private void dettach_Identity(Identity entity)
    {
        entity.IdentityGroups.Remove(this);
    }
}

[DataContract()]
[XmlType(TypeName = "Group")]
public class Group : Identity
{
    public override void Initialize()
    {
        throw new NotImplementedException();
    }
}

but the ToXml() response something like this

<Person xmlns:xsi="************" xmlns:xsd="******" ID="******" Name="*****"/><AccessControlEntries/></Person>

but what i want is something like this

<Person Id="****" Name="***" Nombre="****"> 
  <AccessControlEntries/> 
  <IdentityGroups/> 
</Person>

© Stack Overflow or respective owner

Related posts about xml-serialization