//XML기본예제
XmlTextWriter myXmlTextWriter = new XmlTextWriter ("newbooks.xml", null);
myXmlTextWriter.Formatting = Formatting.Indented;
myXmlTextWriter.WriteStartDocument(false);
myXmlTextWriter.WriteDocType("bookstore", null, "books.dtd", null);
myXmlTextWriter.WriteComment("This file represents another fragment of a book store inventory database");
myXmlTextWriter.WriteStartElement("bookstore");
myXmlTextWriter.WriteStartElement("book", null);
myXmlTextWriter.WriteAttributeString("genre","autobiography");
myXmlTextWriter.WriteAttributeString("publicationdate","1979");
myXmlTextWriter.WriteAttributeString("ISBN","0-7356-0562-9");
myXmlTextWriter.WriteElementString("title", null, "The Autobiography of Mark Twain");
myXmlTextWriter.WriteStartElement("Author", null);
myXmlTextWriter.WriteElementString("first-name", "Mark");
myXmlTextWriter.WriteElementString("last-name", "Twain");
myXmlTextWriter.WriteEndElement();
myXmlTextWriter.WriteElementString("price", "7.99");
myXmlTextWriter.WriteEndElement();
myXmlTextWriter.WriteEndElement();
//Write the XML to file and close the myXmlTextWriter
myXmlTextWriter.Flush();
myXmlTextWriter.Close();
//XML실제 예제
string saveFile = (path.Trim().Substring(path.Length - 1, 1) == "\\" ? path + fileName : path + "\\" + fileName);
XmlTextWriter xwriter = new XmlTextWriter(saveFile, null);
xwriter.Formatting = Formatting.Indented;
xwriter.WriteStartDocument();
xwriter.WriteStartElement("transInfo");
xwriter.WriteElementString("dataKind", dataKind);
xwriter.WriteElementString("tableName", tableName);
xwriter.WriteElementString("receiveDate", today);
xwriter.WriteStartElement("contents", null);
string _tmpcontent = (contents.Trim().Substring(contents.Length - 1, 1) == WinsConst.ROWCONST.ToString() ? contents.Remove(contents.Length - 1) : contents);
//WinsConst.ROWCONST에 지정된 값으로 data1에 배열로 저장된 만큼 루프를 돌려서 XML저장
string[] data1 = _tmpcontent.Split(WinsConst.ROWCONST);
for (int i = 0; i < data1.Length; i++)
{
//Split가 두번 나오는데 크게 한번 나눠주고 다시 세부로 나눠준다.(XML형식때문에 지정한 루프)
string[] data2 = data1[i].Split(WinsConst.COLCONST);
xwriter.WriteStartElement("content", null);
for (int j = 0; j < data2.Length; j++)
{
xwriter.WriteElementString(colName[j], data2[j]);
}
xwriter.WriteEndElement();
}
xwriter.WriteEndElement();
xwriter.WriteEndDocument();
xwriter.Flush();
xwriter.Close();