posted by 써니루루 2007. 3. 26. 11:14

(불러들이는 XML 파일은 이전에 포스트한 글을 참조한다)

이 글에서는 Xml 문서를 읽어들여 값을 수정하는 예제를 보여준다.
.InnerText와 SetAttribute()를 보도록 하자.

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

using System.Xml;
using System.IO;

namespace CH11
{
    class p568_AddNewContent
    {
        static void Main()
        {
            string filePath = @"..\..\booklist.xml";
            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(filePath);

            XmlElement eBookList = xDoc.DocumentElement;

            XmlElement eFirstBook = (XmlElement)eBookList.FirstChild;
            XmlElement eTitle = (XmlElement)eFirstBook.ChildNodes[0];
            eTitle.InnerText = ".NET 개발자를 위한 XML";
            eFirstBook.SetAttribute("kind", "프로그래밍 언어");

            StringWriter strWriter = new StringWriter();
            xDoc.Save(strWriter);

            Console.WriteLine(strWriter.ToString());
        }
    }
}

Ref. .NET 닷넷 개발자를 위한 XML p.570