xml파일을 각 ElementNode별로 읽고, XmlDocument를 생성하고 저장하는 sample code
XmlDocument에 새로운 Element를 생성하는 것은 생성후에 XmlDocument에 넣는 것이 아니라, XmlDocument를 통해 객체를
생성하고, 생성된 객체에 값을 넣은 이후에 XmlDocument에 Append하는 과정을 거쳐야지 된다는 것을 기억하면 매우 쉬운 소스다.
XmlDocument에 새로운 Element를 생성하는 것은 생성후에 XmlDocument에 넣는 것이 아니라, XmlDocument를 통해 객체를
생성하고, 생성된 객체에 값을 넣은 이후에 XmlDocument에 Append하는 과정을 거쳐야지 된다는 것을 기억하면 매우 쉬운 소스다.
001.
/****************************** Module Header ******************************\
002.
* Module Name: Program.cs
003.
* Project: CSXmlGeneral
004.
* Copyright (c) Microsoft Corporation.
005.
*
006.
* This C# sample project shows how to read a XML file by using XmlTextReader
007.
* or XmlNodeReader. It also shows, instead of using forward-only reader, how
008.
* to read, modify, and update Xml element using the XmlDocument class. This
009.
* class will load the whole document into memory for modification and we can
010.
* save the modified XML file to the file system.
011.
*
012.
* The XML file used by the demo has this format:
013.
*
014.
* <catalog>
015.
* <book id="bk101">
016.
* <author>Gambardella, Matthew</author>
017.
* <title>XML Developer's Guide</title>
018.
* <genre>Computer</genre>
019.
* <price>44.95</price>
020.
* <publish_date>2000-10-01</publish_date>
021.
* <description>
022.
* An in-depth look at creating applications
023.
* with XML.
024.
* </description>
025.
* </book>
026.
* <book>
027.
* ...
028.
* </book>
029.
* <catalog>
030.
*
031.
* This source is subject to the Microsoft Public License.
032.
* See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
033.
* All other rights reserved.
034.
*
035.
* THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
036.
* EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
037.
* WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
038.
\***************************************************************************/
039.
040.
#region Using directives
041.
using
System;
042.
using
System.Collections.Generic;
043.
using
System.Linq;
044.
using
System.Text;
045.
using
System.Xml;
046.
#endregion
047.
048.
049.
class
Program
050.
{
051.
static
void
Main(
string
[] args)
052.
{
053.
/////////////////////////////////////////////////////////////////////
054.
// Read XML document using the XmlTextReader class.
055.
//
056.
057.
// The XmlTextReader acts as a reader pointer that only moves forward.
058.
// Because it always moves forward and read a piece of data into
059.
// memory buffer, it has better performance than the XmlDocument
060.
// class which loads the whole document into memory.
061.
062.
Console.WriteLine(
"Loading XML using XmlTextReader..."
);
063.
064.
XmlTextReader xmlTextReader =
new
XmlTextReader(@
"Books.xml"
);
065.
xmlTextReader.WhitespaceHandling = WhitespaceHandling.None;
066.
067.
while
(xmlTextReader.Read())
068.
{
069.
if
(xmlTextReader.Name ==
"book"
)
070.
{
071.
Console.WriteLine(xmlTextReader.GetAttribute(
"id"
) +
": "
);
072.
073.
xmlTextReader.Read();
074.
string
author = xmlTextReader.ReadElementContentAsString();
075.
string
title = xmlTextReader.ReadElementContentAsString();
076.
string
genre = xmlTextReader.ReadElementContentAsString();
077.
string
price = xmlTextReader.ReadElementContentAsString();
078.
string
publishDate = xmlTextReader.ReadElementContentAsString();
079.
string
description = xmlTextReader.ReadElementContentAsString();
080.
081.
Console.WriteLine(genre +
" book \""
+ title +
"\" written by \""
+
082.
author +
"\", published on "
+ publishDate);
083.
Console.WriteLine(description);
084.
}
085.
}
086.
087.
xmlTextReader.Close();
088.
089.
090.
/////////////////////////////////////////////////////////////////////
091.
// Read XML document using the XmlDocument and XmlNodeReader classes.
092.
//
093.
094.
// XmlNodeReader is similar to XmlTextReader but accepts an XmlNode
095.
// instance as target to read. The following code shows how to use
096.
// XmlDocument and XmlNodeReader to retrieve XML information. It is
097.
// also a forward-only reader.
098.
099.
Console.WriteLine(
"Loading XML using XmlDocument & XmlNodeReader..."
);
100.
101.
XmlDocument xmlDocument =
new
XmlDocument();
102.
xmlDocument.Load(@
"Books.xml"
);
103.
XmlNodeList xmlNodes = xmlDocument.GetElementsByTagName(
"book"
);
104.
foreach
(XmlNode node
in
xmlNodes)
105.
{
106.
Console.WriteLine(node.Attributes[
"id"
].Value +
":"
);
107.
108.
XmlNodeReader xmlNodeReader =
new
XmlNodeReader(node);
109.
xmlNodeReader.Read();
110.
xmlNodeReader.Read();
111.
string
author = xmlNodeReader.ReadElementContentAsString();
112.
string
title = xmlNodeReader.ReadElementContentAsString();
113.
string
genre = xmlNodeReader.ReadElementContentAsString();
114.
string
price = xmlNodeReader.ReadElementContentAsString();
115.
string
publishDate = xmlNodeReader.ReadElementContentAsString();
116.
string
description = xmlNodeReader.ReadElementContentAsString();
117.
118.
Console.WriteLine(genre +
" book \""
+ title +
"\" written by \""
+
119.
author +
"\", published on "
+ publishDate);
120.
Console.WriteLine(description);
121.
}
122.
123.
124.
/////////////////////////////////////////////////////////////////////
125.
// Make changes to the XmlDocument.
126.
//
127.
128.
// Modify a node value by first calling SelectSingleNode to navigate
129.
// to that node and by setting its InnerText property to change its
130.
// content.
131.
XmlNode nodeToModify = xmlDocument.DocumentElement.SelectSingleNode(
132.
"book/genre"
);
133.
nodeToModify.InnerText =
"XML Tech"
;
134.
135.
// Add a new XML node. In XML programming, we always call
136.
// XMLDocument.Create*** to create an attribute or element. After
137.
// that, we can add it into where we want by calling Node.AppendChild
138.
XmlElement newElement = xmlDocument.CreateElement(
"book"
);
139.
XmlAttribute newAttribute = xmlDocument.CreateAttribute(
"id"
);
140.
newAttribute.Value =
"bk103"
;
141.
XmlElement authorElement = xmlDocument.CreateElement(
"author"
);
142.
authorElement.InnerText =
"Mark Russinovich,David Solomon,Alex Ionecsu"
;
143.
XmlElement titleElement = xmlDocument.CreateElement(
"title"
);
144.
titleElement.InnerText =
"Windows Internals, 5th edition"
;
145.
XmlElement genreElement = xmlDocument.CreateElement(
"genre"
);
146.
genreElement.InnerText =
"Windows Server 2008"
;
147.
XmlElement priceElement = xmlDocument.CreateElement(
"price"
);
148.
priceElement.InnerText =
"69.99"
;
149.
XmlElement publishDateElement = xmlDocument.CreateElement(
"publish_date"
);
150.
publishDateElement.InnerText =
"2009-6-17"
;
151.
XmlElement descriptionElement = xmlDocument.CreateElement(
"description"
);
152.
descriptionElement.InnerText =
"Windows Internals, 5th edition is the"
+
153.
" update to Windows Internals, 4th edition to cover Windows Vista"
+
154.
" and Windows Server 2008 (32-bit and 64-bit)."
;
155.
156.
newElement.Attributes.Append(newAttribute);
157.
newElement.AppendChild(authorElement);
158.
newElement.AppendChild(titleElement);
159.
newElement.AppendChild(genreElement);
160.
newElement.AppendChild(priceElement);
161.
newElement.AppendChild(publishDateElement);
162.
newElement.AppendChild(descriptionElement);
163.
xmlDocument.DocumentElement.AppendChild(newElement);
164.
165.
// Save the changes
166.
xmlDocument.Save(
"Modified Books.xml"
);
167.
168.
// XmlLDocument does not have Close or Dispose method because it is
169.
// an in-memory representation of an XML document. Once read, the
170.
// file is no-longer needed.
171.
}
172.
}
173.
</catalog></catalog>
01.
<!--?xml version="1.0" encoding="utf-8" ?-->
02.
<
catalog
>
03.
<
book
id
=
"bk101"
>
04.
<
author
>Gambardella, Matthew</
author
>
05.
<
title
>XML Developer's Guide</
title
>
06.
<
genre
>Computer</
genre
>
07.
<
price
>44.95</
price
>
08.
<
publish_date
>2000-10-01</
publish_date
>
09.
<
description
>
10.
An in-depth look at creating applications
11.
with XML.
12.
</
description
>
13.
</
book
>
14.
<
book
id
=
"bk102"
>
15.
<
author
>Ralls, Kim</
author
>
16.
<
title
>Midnight Rain</
title
>
17.
<
genre
>Fantasy</
genre
>
18.
<
price
>5.95</
price
>
19.
<
publish_date
>2000-12-16</
publish_date
>
20.
<
description
>
21.
A former architect battles corporate zombies,
22.
an evil sorceress, and her own childhood to become queen
23.
of the world.
24.
</
description
>
25.
</
book
>
26.
</
catalog
>