piątek, 18 listopada 2011

C# Zapis do pliku XML

Przykład generowania pliku XML.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;

namespace NL_XMLExample
{
    class Program
    {
        static void Main(string[] args)
        {
            XmlTextWriter XmlWriter = new XmlTextWriter("C:\\TEMP\\out.xml", null);
            
            XmlWriter.WriteStartDocument();
            
            XmlWriter.WriteComment("This is the comments.");

            XmlWriter.WriteStartElement("BOOKS");
            
            XmlWriter.WriteAttributeString("LANGUAGE", "Any");

            XmlWriter.WriteStartElement("BOOK");
            XmlWriter.WriteElementString("TITLE", "this is the title.");
            XmlWriter.WriteElementString("AUTHOR", "I am the author.");
            XmlWriter.WriteElementString("PUBLISHER", "who is the publisher.");
            XmlWriter.WriteEndElement();

            XmlWriter.WriteStartElement("BOOK");
            XmlWriter.WriteElementString("TITLE", "this is the title 2.");
            XmlWriter.WriteElementString("AUTHOR", "I am the author 2.");
            XmlWriter.WriteElementString("PUBLISHER", "who is the publisher 2.");
            XmlWriter.WriteEndElement();

            XmlWriter.WriteEndElement();
            
            XmlWriter.WriteEndDocument();
            XmlWriter.Close();            
        }
    }
}
Wynik.



    
        this is the title.
        I am the author.
        who is the publisher.
    
    
        this is the title 2.
        I am the author 2.
        who is the publisher 2.