469,608 Members | 2,134 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,608 developers. It's quick & easy.

Create XML File

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
Sep 6 '08 #1
1 1180
shapper wrote:
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).

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Sep 7 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Frank Millman | last post: by
4 posts views Thread by I_AM_DON_AND_YOU? | last post: by
5 posts views Thread by Michael Sperlle | last post: by
3 posts views Thread by brook | last post: by
15 posts views Thread by lxyone | last post: by
reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.