Create XML File

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
 
M

Martin Honnen

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).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top