Adding XML element to existing XML document in C#

10 07 2009

I was looking around for a good tutorial/example on how to add an XML node to an exisitant XML document but never really found one that I needed. Here’s my version:

Let’s say this is your existing XML Document:

<?xml version="1.0"?>
          <add key="aKey1" value=""/>
          <add key="aKey2" value=""/>
          <add key="aKey3" value=""/>
          <add key="aKey4" value=""/>
               <add verb="*" path="*.vjsproj" type="System.Web.HttpForbiddenHandler"/>

And you wanted to add the following element under the <appSettings> section:

<add key="someKey" value="someValue" />

Here’s the C# code you’d need:

XmlDocument xDoc = new XmlDocument();
XmlNode xNode = xDoc.CreateNode(XmlNodeType.Element, "add", "");
XmlAttribute xKey = xDoc.CreateAttribute("key");
XmlAttribute xValue = xDoc.CreateAttribute("value");
xKey.Value = "someKey";
xValue.Value = "someValue";

And there you have it! Your XML file will now include your new node!



One response

18 08 2009
Onyxtek Software Solutions, LLC

here’s a pretty good example too: (scans a directory and lists out files into an xml doc) this is really useful for LINQ to XML to create data sets to make a silverlight 3/Flash gallery

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;

namespace DirScanner
class Program
static void Main(string[] args)
XmlDocument xmlDoc = new XmlDocument();
XmlElement contentElement = xmlDoc.CreateElement(“content”);
ProcessDir(@”C:\Users\OnyxtekSoftwareSolutions\Documents\Development\gallery”, 1, contentElement, xmlDoc);
const int HowDeepToScan = 4;

public static void ProcessDir(string sourceDir, int recursionLvl, XmlElement contentElement, XmlDocument xmlDoc)
if (recursionLvl <= HowDeepToScan)
// Number holder //
int descText = 0;
// Process the list of files found in the directory.
string[] fileEntries = Directory.GetFiles(sourceDir);
foreach (string fileName in fileEntries)
XmlElement itemElement = xmlDoc.CreateElement("item");

XmlElement imagesUrl = xmlDoc.CreateElement("image");
imagesUrl.InnerText = Path.GetFileName(fileName);

XmlElement thumbUrl = xmlDoc.CreateElement("thumb");
thumbUrl.InnerText = "/thumbImages/" + Path.GetFileName(fileName);

XmlElement descriptionTxt = xmlDoc.CreateElement("description");
descriptionTxt.InnerText = "Photo" + descText;
//descriptionTxt.InnerText = Path.GetFileName(fileName);

// Recurse into subdirectories of this directory.
string[] subdirEntries = Directory.GetDirectories(sourceDir);
foreach (string subdir in subdirEntries)
// Do not iterate through reparse points
if ((File.GetAttributes(subdir) &
FileAttributes.ReparsePoint) !=

ProcessDir(subdir, recursionLvl + 1, contentElement, xmlDoc);


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Get every new post delivered to your Inbox.

%d bloggers like this: