PC Review


Reply
Thread Tools Rate Thread

Create XML File

 
 
shapper
Guest
Posts: n/a
 
      6th Sep 2008
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
 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      7th Sep 2008
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/
 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I create a .bat file to create a shortcut on the desktop? Jenny Microsoft Excel Programming 0 19th Sep 2008 02:39 PM
Can not create a new file with File.Create -- throws exception:Access Denied thephatp Microsoft C# .NET 4 30th Mar 2008 03:48 PM
Solution to: Can't create file:.xls. Right click folder you want to create the file in and then click properties on the shortcut menu to check your permissions on this folder Ky_fanatic Microsoft Outlook Discussion 1 8th Dec 2006 06:15 PM
Drag and drop to create a link to a file in a cell of an Excel file paulaugust2003@yahoo.com Microsoft Excel Discussion 2 18th Sep 2005 03:44 PM
Which of your products can be used to create / burn a web to a cd or dvd and also create an autostart file / cd? Windows XP New Users 5 18th Jul 2005 06:47 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:39 AM.