
The Target
The layout of item section in the xml file I was shooting for looks like this:
<item>
<title>Title</title>
<link>URL</link>
<dc:date>Properly Formatted Date</dc:date>
<dc:creator>Email Address</dc:creator>
<description>
<content:encoded><![CDATA[Data]]></content:encoded>
</description>
</item>
I use two namespaces for the
dc:date, dc:creator, and
content:encoded tags:
http://purl.org/dc/elements/1.1/
http://purl.org/rss/1.0/modules/content/
The Process
Ignoring for a moment the nuts and bolts of opening an xml file for appending, the building of a new node for insertion is a very simple task:
// Create the item element we're adding
XmlElement newItem = xmldoc.CreateElement("item");
Ultimately, the new item created will be inserted into the xml stream.
The
title and
link items are simple, and are created identically to the one above. The only addition is to add the content to the element, and then append the newly created element to our item:
// Create Title element, and append to the item
XmlElement titleElement = xmldoc.CreateElement("title");
titleElement.InnerText = "Some Title";
newItem.AppendChild(titleElement);
The
creator and
date elements are done similarly, with the addition of entering the namespace in the overload of the CreateElement method:
// Create the Creator element, using the dc namespace and append to the item
XmlElement creatorElement = xmldoc.CreateElement("dc:creator", "http://purl.org/dc/elements/1.1/");
creatorElement.InnerText = "Your email address";
newItem.AppendChild(creatorElement);
The most complex part of the item is the description. I made it more difficult because of the requirement for using html in the element. After seeing the code listed above, the description element should be easy to follow.The addition to this construct is the addition of an XmlCDataSection for the marked-up text.
// so first create a description element
XmlElement descriptionElement = xmldoc.CreateElement("description");
// Create the content element, using the content namespace
XmlElement contentElement = xmldoc.CreateElement("content:encoded", "http://purl.org/rss/1.0/modules/content/");
// Create a DataSection to populate the content section
XmlCDataSection cdataSection = xmldoc.CreateCDataSection("HTML Text goes here");
// Append the Data Section to the content element and the content element to the description element
contentElement.AppendChild(cdataSection);
descriptionElement.AppendChild(contentElement);
// and finally, the description element gets appended to the item
newItem.AppendChild(descriptionElement);
At this point, our newly created item is fully populated and ready to be inserted in the xml stream. To determine the insertion point, let's back out a bit and take a more global look at the xml file itself, leaving out all channel and item content:
<?xml version="1.0"?>
<rss version="2.0">
<channel>
<item>
</item>
<item>
</item>
<item>
</item>
</channel>
</rss>
Our stream may have many <item> elements, but only one <channel> element, so we will use that information to find the insertion point:
// Now we're ready to insert our element, but need to find the insertion point
// enumerate the "channel" elements... even though we only have one, it will be item 0
XmlNodeList xmlnode = xmldoc.GetElementsByTagName("channel");
// and insert our new item after the last child of "channel",
//which will put it after the last "item" currently in there
XmlNode currNode = xmlnode[0].InsertAfter(newItem, xmlnode[0].LastChild);
The mundane portions of the code involve opening the xml file as a Filestream for reading, and then opening another Filestream to the same file for writing when we're finished creating and appending our items.
The only change you should have to make to the code is the email address for the
creator element. The date and time is formatted according to the system clock.
The code was built as a "File" project in VS2005, and as such should not be a problem.
I found quite a few sites helpful during my search for information, the stand-out ones are:
An article on CodeGuru on
Manipulating XML File Data Using C#An article from
Morrowland that got me off center with nested elements
This page from W3 Schools discussing the CData element. While not code-specific to the project, it is good information
