The interaction between the user and the application is done by the information displayed on the screen. While displaying the messages on the screen is quite simple managing them can be quite hazardous. In this article I will explain that how you can save and access the messages from an XML file.

Introduction:

The interaction between the user and the application is done by the information displayed on the screen. While displaying the messages on the screen is quite simple managing them can be quite hazardous. In this article I will explain that how you can save and access the messages from an XML file.

Why XML File?

The first question that comes to our mind is that why an XML file? Why not a database? But let's first see what messages are we talking about.

The message above is displayed after you post on the GridViewGuy forums. Now, let's talk about that why we are not storing these messages in the database. The main reason of not storing the information on the database is the overhead caused by the data access. If we have 10-20 messages on the page it is possible that we need 10-20 data accesses. This will cut down performance significantly. The other reason is that displaying messages to the user does not require any operations that involve database. These operations can include sorting, searching and ordering records.

Structure of the XML File:

The good thing about the XML File is that you define your own structure. I like to keep my message XML File very simple. Take a look at the XML File below:

<?xml version="1.0" encoding="utf-8" ?>

<Messages>

<RegisterationSuccessfull>

Your account has been created successfully.

</RegisterationSuccessfull>

<RegisterationFailure>

This account is already registered.

</RegisterationFailure>

<CategoryAddedSuccess>

New Category has been added.

</CategoryAddedSuccess>

<CategoryAddedFailure>

Error: Category has not been added.

</CategoryAddedFailure>

<FileAlreadyExists>

Error: This file already exists.

</FileAlreadyExists>

</Messages>

As, you can see that all my messages comes under the <messages> tag. Each message is a different element which contains the actual message.

Accessing the Messages:

It is very important that you access the XML File correctly. There are many ways to read or parse through the XML file. Each of the way has its own benefits and downfalls. Like if you are using an XmlReader object to access and read the XML file then it will be bad for performance. The reason is that you will have to go through each and every element in the XML file until you find the element you are looking for.

A good way to access the XML file is by the use of XmlDocument object. The XmlDocument object remembers the structure and elements of an XML file and hence you don't need to parse the complete file in order to locate the correct element. Take a look at the code below which uses XmlDocument object to locate an element.

private const string MESSAGES_FILE_PATH = "~/Messages/UI_Messages.xml";

private const string XML_DOCUMENT = "XmlDocument";

public static string GetMessageByElementName(string elementName)

{

string message = String.Empty;

XmlDocument xmlDoc = null;

if (HttpContext.Current.Cache[XML_DOCUMENT] == null)

{

xmlDoc = new XmlDocument();

xmlDoc.Load(HttpContext.Current.Server.MapPath(MESSAGES_FILE_PATH));

// insert into cache object

HttpContext.Current.Cache.Insert(XML_DOCUMENT, xmlDoc, new System.Web.Caching.CacheDependency(HttpContext.Current.Server.MapPath(MESSAGES_FILE_PATH)));

}

// retrieves the value from the cache object

xmlDoc = (XmlDocument)HttpContext.Current.Cache[XML_DOCUMENT];

message = ((XmlNode)((XmlNodeList)xmlDoc.GetElementsByTagName(elementName)).Item(0)).InnerText;

return message;

}

 

The method GetMessageByElementName takes a single parameter, elementName. Once, it has the element name it simply looks up the text associated with that element. As, you might have also noticed that I have used Caching to increase the performance of the application. Caching is set dependent on the XML file; this means that when the XML file changes the cache expires and new content is loaded into memory.

Displaying Messages:

Displaying messages is quite simple. All you need to do is to call the GetMessageByElementName with the correct elementName and that's it. Take a look at the code below which displays the messages which are to be displayed when a new category is added to the database.

Category category = new Category();

category.CategoryName = txtCategoryName.Text;

bool result = CategoryManager.Save(category);

if (result)

lblMessage.Text = MessageManager.GetMessageByElementName("CategoryAddedSuccess");

else

lblMessage.Text = MessageManager.GetMessageByElementName("CategoryAddedFailure");

Conclusion:

In this article we saw that how we can use the XML file to display information to the user. Among other benefits one of the biggest benefit is that when you want to change the message displayed on the screen then all you need to do is to change it in the XML file. This way you don't even have to build the application to view the changes.

I hope you liked the article, happy coding!