posted by 방랑군 2009. 9. 24. 13:47


App.config

<?xml version="1.0" encoding="utf-8" ?>

<configuration>


  <configSections>

    <section name="unity" type="Console_Config_User.UserInfo,Console_Config_User" />

  </configSections>


  <unity>

    <NAME>황승재</NAME>

    <ADDRESS>염창동</ADDRESS>

    <HP>016-647-0678</HP>

  </unity>

</configuration>


unity.xml - 객체만들 부분만 뽑아서 XML 만든다.

<?xml version="1.0" encoding="utf-8" ?>

<unity>

  <NAME>황승재</NAME>

  <ADDRESS AD1="서울시" AD2="염창동" AD3="동아3차아파트" AD4="307동 1101호" />

  <HP>016-647-0678</HP>

</unity> 



XSD.EXE 로 XSD 파일 만들고 CLASS 객체 파일 만든다.

>xsd.exe unity.xml


>xsd.exe unity.xsd /c


unity.cs

//------------------------------------------------------------------------------

// <auto-generated>

//     This code was generated by a tool.

//     Runtime Version:2.0.50727.3082

//

//     Changes to this file may cause incorrect behavior and will be lost if

//     the code is regenerated.

// </auto-generated>

//------------------------------------------------------------------------------


using System.Xml.Serialization;


// 

// This source code was auto-generated by xsd, Version=2.0.50727.1432.

// 

namespace Console_Config_User

{


    /// <remarks/>

    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.1432")]

    [System.SerializableAttribute()]

    [System.Diagnostics.DebuggerStepThroughAttribute()]

    [System.ComponentModel.DesignerCategoryAttribute("code")]

    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]

    [System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]

    public partial class unity

    {


        private string nAMEField;


        private string hpField;


        private unityADDRESS[] aDDRESSField;


        /// <remarks/>

        [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]

        public string NAME

        {

            get

            {

                return this.nAMEField;

            }

            set

            {

                this.nAMEField = value;

            }

        }


        /// <remarks/>

        [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]

        public string HP

        {

            get

            {

                return this.hpField;

            }

            set

            {

                this.hpField = value;

            }

        }


        /// <remarks/>

        [System.Xml.Serialization.XmlElementAttribute("ADDRESS", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]

        public unityADDRESS[] ADDRESS

        {

            get

            {

                return this.aDDRESSField;

            }

            set

            {

                this.aDDRESSField = value;

            }

        }

    }


    /// <remarks/>

    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.1432")]

    [System.SerializableAttribute()]

    [System.Diagnostics.DebuggerStepThroughAttribute()]

    [System.ComponentModel.DesignerCategoryAttribute("code")]

    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]

    public partial class unityADDRESS

    {


        private string aD1Field;


        private string aD2Field;


        private string aD3Field;


        private string aD4Field;


        /// <remarks/>

        [System.Xml.Serialization.XmlAttributeAttribute()]

        public string AD1

        {

            get

            {

                return this.aD1Field;

            }

            set

            {

                this.aD1Field = value;

            }

        }


        /// <remarks/>

        [System.Xml.Serialization.XmlAttributeAttribute()]

        public string AD2

        {

            get

            {

                return this.aD2Field;

            }

            set

            {

                this.aD2Field = value;

            }

        }


        /// <remarks/>

        [System.Xml.Serialization.XmlAttributeAttribute()]

        public string AD3

        {

            get

            {

                return this.aD3Field;

            }

            set

            {

                this.aD3Field = value;

            }

        }


        /// <remarks/>

        [System.Xml.Serialization.XmlAttributeAttribute()]

        public string AD4

        {

            get

            {

                return this.aD4Field;

            }

            set

            {

                this.aD4Field = value;

            }

        }

    }


    /// <remarks/>

    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.1432")]

    [System.SerializableAttribute()]

    [System.Diagnostics.DebuggerStepThroughAttribute()]

    [System.ComponentModel.DesignerCategoryAttribute("code")]

    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]

    [System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]

    public partial class NewDataSet

    {


        private unity[] itemsField;


        /// <remarks/>

        [System.Xml.Serialization.XmlElementAttribute("unity")]

        public unity[] Items

        {

            get

            {

                return this.itemsField;

            }

            set

            {

                this.itemsField = value;

            }

        }

    }

}



UserInfo.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;


using System.Configuration;

using System.Xml;


namespace Console_Config_User

{

    class UserInfo : System.Configuration.IConfigurationSectionHandler

    {


#region IConfigurationSectionHandler 멤버


public object Create(object parent, object configContext, System.Xml.XmlNode section)

{

    //throw new NotImplementedException();

    XmlDocument doc = new XmlDocument();

    doc.LoadXml(section.OuterXml);

    Console_Config_User.unity objClass = new unity();

    objClass = (unity)xxml.ToInstance(doc, typeof(unity));


    return objClass;

}


#endregion

    }

}



Program.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;


using System.Configuration;


namespace Console_Config_User

{

    class Program

    {

        static void Main(string[] args)

        {


            //object config = ConfigurationManager.GetSection("unity");


            unity container = new unity();

            container = (unity)ConfigurationManager.GetSection("unity");

            

        }

    }

}




결과.