Example - XML with collections

For this configuration XML:
<family name="The Does">
    <location city="New York"/>
    <website url="http://thedoes.com"/>
    <members>
        <member first-name="John" last-name="Doe" title="Father"/>
        <member first-name="Jane" last-name="Doe" title="Mother"/>
        <member first-name="Jack" last-name="Doe" title="Son" minor="True"/>
        <member first-name="Jill" last-name="Doe" title="Daughter" minor="True"/>
    </members>
</family>
.NET Configuration Code Generator generates the following C# code:
//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.235
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------



public sealed class Family {
    
    private static FamilySection _config;
    
    static Family() {
        _config = ((FamilySection)(global::System.Configuration.ConfigurationManager.GetSection("family")));
    }
    
    private Family() {
    }
    
    public static FamilySection Config {
        get {
            return _config;
        }
    }
}

public sealed class FamilySection : System.Configuration.ConfigurationSection {
    
    [System.Configuration.ConfigurationPropertyAttribute("name", IsRequired=true)]
    public string Name {
        get {
            return ((string)(this["name"]));
        }
        set {
            this["name"] = value;
        }
    }
    
    [System.Configuration.ConfigurationPropertyAttribute("location")]
    public LocationElement Location {
        get {
            return ((LocationElement)(this["location"]));
        }
    }
    
    [System.Configuration.ConfigurationPropertyAttribute("website")]
    public WebsiteElement Website {
        get {
            return ((WebsiteElement)(this["website"]));
        }
    }
    
    [System.Configuration.ConfigurationPropertyAttribute("members")]
    public MembersElement Members {
        get {
            return ((MembersElement)(this["members"]));
        }
    }
    
    public sealed class LocationElement : System.Configuration.ConfigurationElement {
        
        [System.Configuration.ConfigurationPropertyAttribute("city", IsRequired=true)]
        public string City {
            get {
                return ((string)(this["city"]));
            }
            set {
                this["city"] = value;
            }
        }
    }
    
    public sealed class WebsiteElement : System.Configuration.ConfigurationElement {
        
        [System.Configuration.ConfigurationPropertyAttribute("url", IsRequired=true)]
        public string Url {
            get {
                return ((string)(this["url"]));
            }
            set {
                this["url"] = value;
            }
        }
    }
    
    public sealed class MembersElement : System.Configuration.ConfigurationElement {
        
        [System.Configuration.ConfigurationPropertyAttribute("member")]
        [System.Configuration.ConfigurationCollectionAttribute(typeof(MemberElement), CollectionType=System.Configuration.ConfigurationElementCollectionType.BasicMap)]
        public MemberElementCollection Member {
            get {
                return ((MemberElementCollection)(this["member"]));
            }
        }
        
        public sealed class MemberElementCollection : System.Configuration.ConfigurationElementCollection {
            
            private string this[int i] {
                get {
                    return this.BaseGet(i);
                }
            }
            
            protected override System.Configuration.ConfigurationElement CreateNewElement() {
                return new MemberElement();
            }
            
            protected override object GetElementKey() {
                return ((MemberElement)(element)).FirstName;
            }
        }
        
        public sealed class MemberElement : System.Configuration.ConfigurationElement {
            
            [System.Configuration.ConfigurationPropertyAttribute("first-name", IsRequired=true)]
            public string FirstName {
                get {
                    return ((string)(this["first-name"]));
                }
                set {
                    this["first-name"] = value;
                }
            }
            
            [System.Configuration.ConfigurationPropertyAttribute("last-name", IsRequired=true)]
            public string LastName {
                get {
                    return ((string)(this["last-name"]));
                }
                set {
                    this["last-name"] = value;
                }
            }
            
            [System.Configuration.ConfigurationPropertyAttribute("title", IsRequired=true)]
            public string Title {
                get {
                    return ((string)(this["title"]));
                }
                set {
                    this["title"] = value;
                }
            }
        }
    }
}
and the following Visual BASIC code:
'------------------------------------------------------------------------------
' <auto-generated>
'     This code was generated by a tool.
'     Runtime Version:4.0.30319.235
'
'     Changes to this file may cause incorrect behavior and will be lost if
'     the code is regenerated.
' </auto-generated>
'------------------------------------------------------------------------------

Option Strict Off
Option Explicit On



Public NotInheritable Class Family
    
    Private Shared _config As FamilySection
    
    Shared Sub New()
        _config = CType(Global.System.Configuration.ConfigurationManager.GetSection("family"),FamilySection)
    End Sub
    
    Private Sub New()
        MyBase.New
    End Sub
    
    Public Shared ReadOnly Property Config() As FamilySection
        Get
            Return _config
        End Get
    End Property
End Class

Public NotInheritable Class FamilySection
    Inherits System.Configuration.ConfigurationSection
    
    <System.Configuration.ConfigurationPropertyAttribute("name", IsRequired:=true)>  _
    Public Property Name() As String
        Get
            Return CType(Me("name"),String)
        End Get
        Set
            Me("name") = value
        End Set
    End Property
    
    <System.Configuration.ConfigurationPropertyAttribute("location")>  _
    Public ReadOnly Property Location() As LocationElement
        Get
            Return CType(Me("location"),LocationElement)
        End Get
    End Property
    
    <System.Configuration.ConfigurationPropertyAttribute("website")>  _
    Public ReadOnly Property Website() As WebsiteElement
        Get
            Return CType(Me("website"),WebsiteElement)
        End Get
    End Property
    
    <System.Configuration.ConfigurationPropertyAttribute("members")>  _
    Public ReadOnly Property Members() As MembersElement
        Get
            Return CType(Me("members"),MembersElement)
        End Get
    End Property
    
    Public NotInheritable Class LocationElement
        Inherits System.Configuration.ConfigurationElement
        
        <System.Configuration.ConfigurationPropertyAttribute("city", IsRequired:=true)>  _
        Public Property City() As String
            Get
                Return CType(Me("city"),String)
            End Get
            Set
                Me("city") = value
            End Set
        End Property
    End Class
    
    Public NotInheritable Class WebsiteElement
        Inherits System.Configuration.ConfigurationElement
        
        <System.Configuration.ConfigurationPropertyAttribute("url", IsRequired:=true)>  _
        Public Property Url() As String
            Get
                Return CType(Me("url"),String)
            End Get
            Set
                Me("url") = value
            End Set
        End Property
    End Class
    
    Public NotInheritable Class MembersElement
        Inherits System.Configuration.ConfigurationElement
        
        <System.Configuration.ConfigurationPropertyAttribute("member"),  _
         System.Configuration.ConfigurationCollectionAttribute(GetType(MemberElement), CollectionType:=System.Configuration.ConfigurationElementCollectionType.BasicMap)>  _
        Public ReadOnly Property Member() As MemberElementCollection
            Get
                Return CType(Me("member"),MemberElementCollection)
            End Get
        End Property
        
        Public NotInheritable Class MemberElementCollection
            Inherits System.Configuration.ConfigurationElementCollection
            
            Private Default ReadOnly Property Item(ByVal i As Integer) As String
                Get
                    Return Me.BaseGet(i)
                End Get
            End Property
            
            Protected Overrides Function CreateNewElement() As System.Configuration.ConfigurationElement
                Return New MemberElement()
            End Function
            
            Protected Overrides Function GetElementKey() As Object
                Return CType(element,MemberElement).FirstName
            End Function
        End Class
        
        Public NotInheritable Class MemberElement
            Inherits System.Configuration.ConfigurationElement
            
            <System.Configuration.ConfigurationPropertyAttribute("first-name", IsRequired:=true)>  _
            Public Property FirstName() As String
                Get
                    Return CType(Me("first-name"),String)
                End Get
                Set
                    Me("first-name") = value
                End Set
            End Property
            
            <System.Configuration.ConfigurationPropertyAttribute("last-name", IsRequired:=true)>  _
            Public Property LastName() As String
                Get
                    Return CType(Me("last-name"),String)
                End Get
                Set
                    Me("last-name") = value
                End Set
            End Property
            
            <System.Configuration.ConfigurationPropertyAttribute("title", IsRequired:=true)>  _
            Public Property Title() As String
                Get
                    Return CType(Me("title"),String)
                End Get
                Set
                    Me("title") = value
                End Set
            End Property
        End Class
    End Class
End Class

Last edited Jul 11, 2011 at 7:43 AM by jeevanjj, version 2

Comments

No comments yet.