<?xml version="1.0" encoding="utf-8" ?>
<booklist>
<book id="b1" kind="컴퓨터">
<title>기초에서 실무까지 XML</title>
<author>신민철</author>
<publisher>프리렉</publisher>
<price>35000</price>
</book>
<book id="b2" kind="소설">
<title>사랑과 전쟁</title>
<author>이사랑</author>
<publisher>전쟁문화사</publisher>
<price>15000</price>
</book>
<book id="b3" kind="잡지">
<title>마이크로 소프트</title>
<author>빌 게이츠</author>
<publisher>마소문화사</publisher>
<price>20000</price>
</book>
<book id="b4" kind="소설">
<title>액션가면부인 바람났네</title>
<author>짱구</author>
<publisher>짱구출판사</publisher>
<price>12000</price>
</book>
</booklist>
위 문서를 C#으로 읽어 들이기 위해 XmlDocument 개체를 이용한다.
using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;using System.Xml;
namespace CH11
{
class p554
{
static void Main()
{
string filePath = @"..\..\booklist.xml";
XmlDocument xDoc = new XmlDocument();
xDoc.Load(filePath);XmlElement eBook = xDoc.DocumentElement;
XmlElement eFirstBook = (XmlElement)eBook.FirstChild;
XmlElement eLastBook = (XmlElement)eBook.LastChild;Console.Write("첫 번째 책 정보 : ");
Console.WriteLine("\t" + "id=" + eFirstBook.GetAttribute("id"));Console.Write("마지막 책 정보 : ");
Console.WriteLine("\t" + "id=" + eLastBook.GetAttribute("id"));
}
}
}
ref. .NET 닷넷 개발자를 위한 XML p554