XmlWriter with XElement

 
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
using System.Text;
using System.IO;
class Program
{
    static void Main()
    {
        using (XmlWriter w = XmlWriter.Create("foo.xml"))
        {
            w.WriteStartElement("log");
            for (int i = 0; i < 10; i++)
            {
                XElement e = new XElement("logentry", new XAttribute("id", i),
                new XElement("date", DateTime.Today.AddDays(-1)), new XElement("source", "test"));
                e.WriteTo(w);
            }
            w.WriteEndElement();
        }
    }
}

The content of foo.xml:

 
<?xml version="1.0" encoding="utf-8"?>
<log>
   <logentry id="0">
   <date>2010-12-17T00:00:00-08:00</date>
   <source>test</source>
   </logentry>
   <logentry id="1">
   <date>2010-12-17T00:00:00-08:00</date>
   <source>test</source>
   </logentry>
   <logentry id="2">
   <date>2010-12-17T00:00:00-08:00</date>
   <source>test</source>
   </logentry>
   <logentry id="3">
   <date>2010-12-17T00:00:00-08:00</date>
   <source>test</source>
   </logentry>
   <logentry id="4">
   <date>2010-12-17T00:00:00-08:00</date>
   <source>test</source>
   </logentry>
   <logentry id="5">
   <date>2010-12-17T00:00:00-08:00</date>
   <source>test</source>
   </logentry>
   <logentry id="6">
   <date>2010-12-17T00:00:00-08:00</date>
   <source>test</source>
   </logentry>
   <logentry id="7">
   <date>2010-12-17T00:00:00-08:00</date>
   <source>test</source>
   </logentry>
   <logentry id="8">
   <date>2010-12-17T00:00:00-08:00</date>
   <source>test</source>
   </logentry>
   <logentry id="9">
   <date>2010-12-17T00:00:00-08:00</date>
   <source>test</source>
   </logentry>
</log>
  
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.