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"?>
<configuration>
     <appSettings>
          <add key="aKey1" value="http://www.google.com"/>
          <add key="aKey2" value="http://www.yahoo.com"/>
          <add key="aKey3" value="http://www.bing.com"/>
          <add key="aKey4" value="http://www.facebook.com"/>
     </appSettings>
     <system.web>
          <httpHandlers>
               <add verb="*" path="*.vjsproj" type="System.Web.HttpForbiddenHandler"/>
          </httpHandlers>
     </system.web>
</configuration>

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();
xDoc.Load("C:\yourXmlFile.xml");
XmlNode xNode = xDoc.CreateNode(XmlNodeType.Element, "add", "");
XmlAttribute xKey = xDoc.CreateAttribute("key");
XmlAttribute xValue = xDoc.CreateAttribute("value");
xKey.Value = "someKey";
xValue.Value = "someValue";
xNode.Attributes.Append(xKey);
xNode.Attributes.Append(xValue);
xDoc.GetElementsByTagName("appSettings")[0].InsertAfter(xNode,
xDoc.GetElementsByTagName("appSettings")[o].LastChild);
xDoc.Save("C:\yourXmlFile.xml");

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

About these ads

Actions

Information

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”);
xmlDoc.AppendChild(contentElement);
ProcessDir(@”C:\Users\OnyxtekSoftwareSolutions\Documents\Development\gallery”, 1, contentElement, xmlDoc);
xmlDoc.Save(@”C:\Users\OnyxtekSoftwareSolutions\Documents\Development\gallery.xml”);
}
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");
contentElement.AppendChild(itemElement);

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

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

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

// 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) !=
FileAttributes.ReparsePoint)

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

}
}
}

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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




Follow

Get every new post delivered to your Inbox.

%d bloggers like this: