Create XML File

  • Thread starter Thread starter shapper
  • Start date Start date
S

shapper

Hello,

How do I create the following XML file at runtime?

<?xml version="1.0" encoding="UTF-8"?>
<gallery>
<album
title="Album Title"
description="Album Description"
lgPath="../MyAlbum/Lg/"
tnPath="../MyAlbum/Tn/"
tn="Tn.jpg">

<img src="Image.jpg" title="image title" caption="image caption" /
</album>
</gallery>

Thanks,
Miguel
 
shapper said:
How do I create the following XML file at runtime?

<?xml version="1.0" encoding="UTF-8"?>
<gallery>
<album
title="Album Title"
description="Album Description"
lgPath="../MyAlbum/Lg/"
tnPath="../MyAlbum/Tn/"
tn="Tn.jpg">

<img src="Image.jpg" title="image title" caption="image caption" /

</album>
</gallery>

You can do that using XmlWriter e.g.
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.NewLineOnAttributes = true;
using (XmlWriter writer = XmlWriter.Create("file.xml", settings))
{
writer.WriteStartDocument();
writer.WriteStartElement("gallery");
writer.WriteStartElement("album");
writer.WriteAttributeString("title", "Album Title");
writer.WriteAttributeString("description", "Album Description");
// write further attributes here
writer.WriteStartElement("img");
writer.WriteAttributeString("src", "Image.jpg");
// write further atrributes here
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndDocument();
}

Other options are using System.Xml.XmlDocument, or with .NET 3.5 to use
LINQ to XML (XDocument or with VB.NET XML literals).
 
Back
Top