상세 컨텐츠

본문 제목

[펌] XmlDocument를 이용한 XML 문서 생성

XML

by happynuri 2008. 6. 18. 11:27

본문

XML 문서를 XmlDocument를 이용해 생성한다.
기본으로 유니코드로 저장하기 때문에 한글이 깨지는 문제가 발생하는데, 이 때문에 이 방법 보다는 XmlWriter를 이용해서 XmlWriter.create를 이용해 문서를 생성하는 방법이 더 나은것 같다.

using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;

using System.Xml;

namespace CH11
{
    class p575_NewDOM
    {
        static void Main()
        {
            XmlDocument xDoc = new XmlDocument();

            XmlElement eBookList = xDoc.CreateElement("booklist");
            xDoc.AppendChild(eBookList);

            XmlElement eBook = xDoc.CreateElement("book");
            XmlElement eTitle = xDoc.CreateElement("title");
            XmlText txtTitle = xDoc.CreateTextNode("XML Programming with .NET");

            eTitle.AppendChild(txtTitle);
            eBook.AppendChild(eTitle);

            eBook.SetAttribute("id", "b10");
            eBook.SetAttribute("kind", "컴퓨터");
            eBookList.AppendChild(eBook);

            xDoc.Save(@"..\..\booklist2.xml");
        }
    }
}

ref. .NET 개발자를 XML p.575

관련글 더보기

댓글 영역