첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
유용한 소스 코드가 있으면 icodebroker@naver.com으로 보내주시면 감사합니다.
블로그 자료는 자유롭게 사용하세요.

728x90
반응형

TestProject.zip
다운로드

▶ MainForm.cs

using System;
using System.IO;
using System.Xml.Serialization;
using System.Windows.Forms;

namespace TestProject
{
    /// <summary>
    /// 메인 폼
    /// </summary>
    public partial class MainForm : Form
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - MainForm()

        /// <summary>
        /// 생성자
        /// </summary>
        public MainForm()
        {
            InitializeComponent();

            this.contactMethodComboBox.SelectedIndex = 1;

            #region 이벤트를 설정한다.

            this.serializeButton.Click   += serializeButton_Click;
            this.deserializeButton.Click += deserializeButton_Click;

            #endregion
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Private
        //////////////////////////////////////////////////////////////////////////////// Event

        #region 직렬화 버튼 클릭시 처리하기 - serializeButton_Click(sender, e)

        /// <summary>
        /// 직렬화 버튼 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void serializeButton_Click(object sender, EventArgs e)
        {
            Person.ContactMethod contactMethod;

            switch(this.contactMethodComboBox.Text)
            {
                case "우편" : contactMethod = Person.ContactMethod.Post;  break;
                case "전화" : contactMethod = Person.ContactMethod.Phone; break;
                default     : contactMethod = Person.ContactMethod.Email; break;
            }

            Person person = new Person
            (
                this.firstNameTextBox.Text,
                this.lastNameTextBox.Text,
                this.streetTextBox.Text,
                this.cityTextBox.Text,
                this.stateTextBox.Text,
                this.zipTextBox.Text,
                this.phone1TextBox.Text,
                this.phone2TextBox.Text,
                this.mail1TextBox.Text,
                this.mail2TextBox.Text,
                contactMethod
            );

            XmlSerializer serializer = new XmlSerializer(typeof(Person));

            using(StringWriter writer = new StringWriter())
            {
                serializer.Serialize(writer, person);

                this.xmlTextBox.Text = writer.ToString();
            }

            this.firstNameTextBox.Clear();
            this.lastNameTextBox.Clear();
            this.streetTextBox.Clear();
            this.cityTextBox.Clear();
            this.stateTextBox.Clear();
            this.zipTextBox.Clear();
            this.phone1TextBox.Clear();
            this.phone2TextBox.Clear();
            this.mail1TextBox.Clear();
            this.mail2TextBox.Clear();

            this.contactMethodComboBox.SelectedIndex = -1;
        }

        #endregion
        #region 역직렬화 버튼 클릭시 처리하기 - deserializeButton_Click(sender, e)

        /// <summary>
        /// 역직렬화 버튼 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void deserializeButton_Click(object sender, EventArgs e)
        {
            XmlSerializer erializer = new XmlSerializer(typeof(Person));

            using(StringReader reader = new StringReader(this.xmlTextBox.Text))
            {
                Person person = (Person)erializer.Deserialize(reader);

                this.firstNameTextBox.Text = person.FirstName;
                this.lastNameTextBox.Text  = person.LastName;
                this.streetTextBox.Text    = person.Street;
                this.cityTextBox.Text      = person.City;
                this.stateTextBox.Text     = person.Street;
                this.zipTextBox.Text       = person.Zip;
                this.phone1TextBox.Text    = person.PhoneArray[0];
                this.phone2TextBox.Text    = person.PhoneArray[1];
                this.mail1TextBox.Text     = person.EmailArray[0];
                this.mail2TextBox.Text     = person.EmailArray[1];

                string contextMethod;

                switch(person.PreferredMethod)
                {
                    case Person.ContactMethod.Post  : contextMethod = "우편"; break;
                    case Person.ContactMethod.Phone : contextMethod = "전화"; break;
                    default                         : contextMethod = "메일"; break;
                }

                this.contactMethodComboBox.Text = contextMethod;
            }

            this.xmlTextBox.Clear();
        }

        #endregion
    }
}
728x90
반응형
Posted by 사용자 icodebroker

댓글을 달아 주세요