How to make soft messages in c#/asp.net
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
Tag Cloud
.net accessibility add ons adwords ajax asp asp.net b2b base blog blogs bookmark bots browse browser browsers c# calendar categories clients client side cloud computing cms collaborative config content management content management system cross browser cross browser issues css design designing dev development development tips ecommerce entity framework epdq facebook favourites froogle galleriffic gallery google hackers how-to ie ie6 ie8 intellectual property internet explorer ip javascript jquery json keywords killer apps layout managing expectations markup mashup merchant center microsoft mvc n2 cms networking new business new ideas nop nopcommerce nop gallery objectivity oen source office applications open source page rankings partnerships payment gateway payment gateways ppc products prototype qtip robots search engines seo slimbox social media social networking soft software source source code spam spiders taxonomy thin client tips tools tricks twitter useful viusal studio vulnerability wai wave web web2.0 web design web development web site creation web sites web tools xml youtube
Browse by Date
Subscribe via RSS
Follow us on...
N-WebDesign news
- Visual Studio 2010 & .NET 4.0
- 22/10/2009
- Windows Seven
- 20/10/2009
- Web Design Portfolio
- 19/10/2009
- You can now follow us on Twitter
- 02/10/2009
- Visit our Facebook page
- 22/09/2009









