본문 바로가기

   
Programming/XML

윈폼 메모 예제 XmlElement

반응형



using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.Xml;

 

namespace WinFormEx

{

       public partial class Ex03 : Form

       {

             private XmlDocument doc;

 

             public Ex03()

             {

                    InitializeComponent();

                    doc = new XmlDocument();

             }

 

             private void Ex03_Load(object sender, EventArgs e)

             {

                    doc.Load("memo.xml");

 

                    ShowXml();

             }

 

             private void ShowXml()

             {

                    comboBox1.Items.Clear();

 

                    XmlDocument doc2 = new XmlDocument();

                    doc2.PreserveWhitespace = true;//**

                    doc2.Load("memo.xml");

 

                    richTextBox1.Text = doc2.OuterXml;

 

                    //메모 제목을 콤보박스에 추가

                    foreach (XmlElement item in doc.DocumentElement.ChildNodes)

                    {

                           MemoItem m = new MemoItem();

                           m.Title = item.FirstChild.InnerText;

                           m.No = item.GetAttribute("no");

 

                           comboBox1.Items.Add(m);

                           comboBox1.DisplayMember = "Title";

                    }

             }

 

             private void button1_Click(object sender, EventArgs e)

             {

                    //문서의 구성요소 생성은 문서객체가 한다.

                    // - Javascript : document

                    //           - document.createElement("태그명");

                    // - C# : XmlDocument

 

                    //1. <item>

                    XmlElement item = doc.CreateElement("item");

 

                    //2. <title>

                    XmlElement title = doc.CreateElement("title");

 

                    //3. <content>

                    XmlElement content = doc.CreateElement("content");

 

                    //4. <regdate>

                    XmlElement regdate = doc.CreateElement("regdate");

 

                    //5. 1 = 2 + 3 + 4

                    item.AppendChild(title);

                    item.AppendChild(content);

                    item.AppendChild(regdate);

 

                    //6. PCDATA 넣기

                    title.InnerText = textBox1.Text;

 

                    XmlText txt = doc.CreateTextNode(textBox2.Text);

                    content.AppendChild(txt);

 

                    regdate.InnerText = DateTime.Now.ToShortDateString();

 

 

                    //7. 속성 <item no="m1">

                    // a. 마지막 메모 가져오기

                    XmlElement lastMemo = doc.DocumentElement.LastChild as XmlElement;

 

                    // b. 메모의 no 속성값 가져오기 -> "m1"

                    string no = lastMemo.GetAttribute("no");

 

                    // c. no +1

                    //  "m1" -> "1"

                    // - 새로운 메모의 번호

                    int index = int.Parse(no.Replace("m", "")) + 1;

 

                    // d. 새로운 메모번호를 <item>속성으로 추가하기

                    item.SetAttribute("no", "m" + index);

 

 

                    //8. <item>을 루트에 추가하기

                    doc.DocumentElement.AppendChild(item);

 

                    //----------------메모리 작업..

 

                    //9. memo.xml에 반영.. 저장하기

                    // - 메모리상의 Xml 트리구조를 모두 문서화시켜서 물리적인 xml파일에 저장

                    doc.Save("memo.xml");

 

 

                    //10.

                    ShowXml();

             }

 

             private void button2_Click(object sender, EventArgs e)

             {

                    //문서 생성하기(memo2.xml)

 

                    //1. doc

                    XmlDocument doc = new XmlDocument();

 

                    //2. 읽어오기, 생성하기..

 

                    //Xml 선언문

                    XmlProcessingInstruction pi = doc.CreateProcessingInstruction("xml", "version='1.0' encoding='utf-8'");

                    //3. 모든 구성요소는 doc이하에 포함

                    doc.AppendChild(pi);

 

 

                    //주석

                    XmlComment cmt = doc.CreateComment("memo2.xml");

                    doc.AppendChild(cmt);

 

                    //루트

                    XmlElement list = doc.CreateElement("list");

                    doc.AppendChild(list);

 

                    //<item>

                    for (int i = 1; i <= 5; i++)

                    {

                           XmlElement item = doc.CreateElement("item");

                           XmlElement title = doc.CreateElement("title");

                           XmlElement content = doc.CreateElement("content");

                           XmlElement regdate = doc.CreateElement("regdate");

 

                           //CDATASection : 이영역은 XML 구성요소가 와도 문법으로 인식을 안한다. 그래서 주로 사용자가 입력한 데이터를 관리할때 사용한다.

                           //title.InnerText = "메모입니다.";//X

                           XmlCDataSection ctitle = doc.CreateCDataSection("<item> 태그 연습중..");

                           title.AppendChild(ctitle);

 

                           //content.InnerText//X

                           XmlCDataSection cContent = doc.CreateCDataSection("<item> 내용입니다...");

                           content.AppendChild(cContent);

 

                           regdate.InnerText = DateTime.Now.ToShortDateString();

 

                           //속성

                           item.SetAttribute("no", "m" + i);

 

                           //결합

                           item.AppendChild(title);

                           item.AppendChild(content);

                           item.AppendChild(regdate);

 

                           list.AppendChild(item);

                    }

 

                    //MessageBox.Show(doc.OuterXml);

                    doc.Save("memo2.xml");

             }

 

             private void button3_Click(object sender, EventArgs e)

             {

                    //선택된 메모 찾기

                    // - 선택된 메모의 no값을 가지고 있는 <item> 찾기

 

                    MemoItem m = comboBox1.SelectedItem as MemoItem;

 

                    XmlElement item = doc.SelectSingleNode(string.Format("//item[@no='{0}']", m.No)) as XmlElement;

 

 

                    //수정하기

                    item.FirstChild.InnerText = "수정함";

 

                    doc.Save("memo.xml");

                    ShowXml();

             }

 

             private void button4_Click(object sender, EventArgs e)

             {

                    //삭제

                    MemoItem m = comboBox1.SelectedItem as MemoItem;

 

                    XmlElement item = doc.SelectSingleNode(string.Format("//item[@no='{0}']", m.No)) as XmlElement;

 

 

                    doc.DocumentElement.RemoveChild(item);

                    doc.Save("memo.xml");

                    ShowXml();

             }

       }

}

 

 

반응형