Sign up for our newsletter

Email Address:

We endorse and use:

We use Code Spaces for our SVN Repository
We use nopCommerce for our open source e-commerce solutions
We use Umbraco for our open source CMS solutions
We use SilverStripe for our open source CMS solutions
We use SourceForge for our open source project hosting
We use the 960.gs css grid for our projects

Valid CSS!

Valid HTML 4.01 Transitional

How to make soft messages in c#/asp.net

Posted by Ed on 30 July 2009 | 0 Comments

Tags: , , , , , , ,

Following on from my recent blog article about creating 'soft'messages in web applications, a few people have asked me how I do it in .NET, so here goes...

Ok, we need a message file. For this we will use an xml file.
We need a few methods in a class to get the message out of the message file.
We need to know a bit about XML.
We need to know a bit about XPath.
We need to know a bit about c#.
We need to have something to say.

Ok, first up, open your favourite xml editor. You can use Visual Studio or notepad or anything in between. Don't use anything like word or TextEdit (Basically, anything that does rich text).

Create an XML file, something like:

<?xml version="1.0" encoding="utf-8" ?>
<messages>
<message id="message">
<content>
<![CDATA[
<p>I am a message</p>
]]>
</content>
</message>
</messages>

This has a very sinple structure. Note I have added a CDATA section, so the markup inside it won't be parsed. This isn't absolutely necessary, but it allows us to add markup within the XML file without worrying about it being meaningful to the parser. Also note, there is a <content> tag defined inside the <message> tag, which may seem little superfluous but this will allow you to add more values later on without changing anything, except the XPath.

Next, open or create a utilities class in c#. (Remember, it goes in the App_Code folder).

The class will look something like:

using System; // Plus some others...

public class UtilitiesClass
{
public UtilitiesClass()
   {
      //
      // TODO: Add constructor logic here
      //
}
}

To play with XML, we need to ensure there are a few libraries added to our reference at the top of the class. We will need:

using System.Text;
using System.Xml;

So, if they aren't there, please add them.

Ok, now to the mechanics of it all.

We need to add a method that will return the message that we want, from the message id. To make this re-usabel, we will add a parameter to the method for the path to the XML file and the XPath to find the right node.

So, this is how I've done it before:

public static string GetXMLNodeValue(string sDoc, string sNodePath)
{
return GetXMLDocument(sDoc).SelectSingleNode(sNodePath).InnerText;
}

...and we also need the GetXMLDocuments() method.

private static XmlDocument GetXMLDocument(string sDoc)
{
XmlDocument xDoc = new XmlDocument();
xDoc.Load(sDoc);
return xDoc;
}

...all this does is get an XML doc and return it to the method that extracts the value from the node we have targetted. You could just add this to the first method, but I like re-usability.

Note: I've not paid any attention to resolving your document root or using Server.MapPath() in this example. Running on localhost or a web server will most likely have a different file structure, so you will need to take this into account when you create your files.

Notice in this example I use static methods. It will work just as well as a non-static method.

That's it. Just add that method to the utilities class (or any other one you want to use) and you can get the text inside the <content> tags ready for output. In a compiled application, all you need to do to change the message is to change the XML file. No re-compile. Cool :)

To make this work in your aplication, just add a call to the class method wherever you want to use the message. For example:

protected void LinkButton_Command(object sender, CommandEventArgs e)
{
literal.Text = UtilitiesClass.GetXMLNodeValue("c:\Inetpub\MySite\xmlfile.xml", "/messages/message[@id=\"message\"]/content/text()"
}

...and that's it.

Note: I've just hacked this out, untested and uncompiled, so there may be a few errors in it. It works fine though, so try it and let's give our clients far better, easier to manage messaging on their web sites... :)

Thanks for reading.


Post your comment

Comments

No one has commented on this page yet.

RSS feed for comments on this page | RSS feed for all comments

Text size: Aa Bb Cc

Follow us on...

N-WebDesign news

Terms :: Privacy :: © N-Web Design 2012