■ XmlTextWriter 클래스를 사용해 XML 문서를 생성하는 방법을 보여준다.
▶ 예제 코드 (C#)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
using System.Xml; string targetFilePath = "c:\\sample.xml"; XmlTextWriter xmlTextWriter = new XmlTextWriter(targetFilePath, null); xmlTextWriter.Formatting = Formatting.Indented; xmlTextWriter.WriteStartDocument(false); xmlTextWriter.WriteDocType("bookstore", null, "books.dtd", null); xmlTextWriter.WriteComment("This file represents another fragment of a book store inventory database"); xmlTextWriter.WriteStartElement("book", null); xmlTextWriter.WriteAttributeString("genre" , "autobiography"); xmlTextWriter.WriteAttributeString("publicationdate", "1979" ); xmlTextWriter.WriteAttributeString("ISBN" , "0-7356-0562-9"); xmlTextWriter.WriteElementString("title", null, "The Autobiography of Mark Twain"); xmlTextWriter.WriteStartElement("Author", null); xmlTextWriter.WriteElementString("first-name", "Mark"); xmlTextWriter.WriteElementString("last-name", "Twain"); xmlTextWriter.WriteEndElement(); xmlTextWriter.WriteElementString("price", "7.99"); xmlTextWriter.WriteEndElement(); xmlTextWriter.Flush(); xmlTextWriter.Close(); |