views:

90

answers:

3

Currently i'm developing a software which uses layered architecture using Visual Studio 2008 and C#. In that system I want to keep all the text messages(that will be shown using message boxes when necessary) in an XML file so that if I update any message I dont have to compile the whole system. How can I accomplish this? What will be the XML file structure? How the messages will be called from C# code? Please be descriptive and give example. Thanks.

+1  A: 

A better choice for your scenario may be a Resource Satellite DLL. This is a separate DLL which contains a set of resoures that are dynamically loaded into your application. This is how many different applications do localization. And most importantly, there are many tools which support this type of feature.

Changing the messages would simply involve changing this DLL. This does result in a compile, but only of the resources and not the system.

JaredPar
A: 

You might want to add your own custom section to your app.config file to store your messages. However this would require you to update the app.config file each time a message changes.

http://msdn.microsoft.com/en-us/library/system.configuration.configurationsection.aspx

Jeremy
I dont want to store messages on app.config file. Rather I want to use another XML file (say, Messages.xml) to store the messages.
Bad Boy
Then you may want to use XML Serialization to convert your XML file of messages into an array of Message objects.http://www.dotnetjunkies.com/QuickStartv20/howto/doc/xmlserialization/RWObjFromXML.aspx
Jeremy
+1  A: 

I would create a class that used a .NET DataSet and DataTable to store the ids and messages (and whatever else you need).

That way the class can use the DataSet.WriteXML() and DataSet.ReadXML() to save/load from any filename you specify and you don't have to worry about "hand crafting" the XML format yourself.

Ron

This sounded like a handy class to have, so I wrote one up as a "proof of concept":

'*************************************************************************
'   Class: MsgFile
'  Author: Ron Savage
'    Date: 05/14/2009
'
' This class is an example for using an XML file to hold messages to be
' displayed by an application.
'
' Modification History
' Date        Init Comment
' 05/14/2009  RS   Created.
'*************************************************************************
Imports Microsoft.VisualBasic
Imports System.IO

Public Class MsgFile
   '*************************************************************************
   ' Class Variables
   '*************************************************************************
   Private MsgFile As String
   Private msgData As DataSet
   Private msgTable As DataTable

   '*************************************************************************
   '     Sub: New()
   '  Author: Ron Savage
   '    Date: 05/14/2009
   '
   ' Creates a new MsgFile from an existing file, or creates an empty one
   ' with the specified file name.
   '*************************************************************************
   Sub New(ByVal msg_File As String)
      Dim srcFile As New FileInfo(msg_File)

      If (srcFile.Exists()) Then
         msgData = New DataSet()

         Load(msg_File)
      Else
         NewFile(msg_File)
      End If
   End Sub

   '*************************************************************************
   '     Sub: NewFile()
   '  Author: Ron Savage
   '    Date: 05/14/2009
   '
   ' Creates a new XML message file.
   '*************************************************************************
   Sub NewFile(ByVal msg_File As String)
      Me.MsgFile = msg_File

      If (IsNothing(msgData)) Then
         msgData = New DataSet("MyAppMessages")
         msgTable = New DataTable("Messages")

         msgTable.Columns.Add(New DataColumn("Id", GetType(System.Int32), Nothing, MappingType.Attribute))
         msgTable.Columns.Add(New DataColumn("Text", GetType(System.String), Nothing, MappingType.Attribute))

         msgData.Tables.Add(msgTable)

         setMessage(0, "New file created")
      End If

      Save()
   End Sub

   '*************************************************************************
   '     Sub: Load()
   '  Author: Ron Savage
   '    Date: 05/14/2009
   '
   ' Loads an existing XML message file into the dataset.
   '*************************************************************************
   Public Sub Load(ByVal msgFileName As String)
      Dim srcFile As FileInfo = Nothing
      Me.MsgFile = msgFileName

      srcFile = New FileInfo(msgFileName)

      msgData.Clear()

      If (srcFile.Exists()) Then
         msgData.ReadXml(msgFileName)

         msgTable = msgData.Tables("Messages")
      End If
   End Sub

   '*************************************************************************
   '     Sub: Save()
   '  Author: Ron Savage
   '    Date: 10/05/2008
   '
   ' This routine saves the DataSet to an XML file.
   '*************************************************************************
   Public Overridable Sub Save()
      If (Not IsNothing(msgData) And Not MsgFile.Equals("")) Then
         Dim fileWriter As StreamWriter = New StreamWriter(MsgFile)


         msgData.WriteXml(fileWriter)

         fileWriter.Close()
      End If
   End Sub

   '*************************************************************************
   '     Sub: getMessage()
   '  Author: Ron Savage
   '    Date: 05/14/2009
   '
   ' This gets the text of the message.
   '*************************************************************************
   Public Function getMessage(ByVal msgId As String) As String
      Dim rowsFound() As System.Data.DataRow
      Dim msgText As String = ""

      If (Not IsNothing(msgTable)) Then
         rowsFound = msgTable.Select("Id = " + msgId.ToString)

         If (rowsFound.Length > 0) Then
            msgText = rowsFound(0).Item("Text").ToString
         End If

      End If
      Return (msgText)
   End Function

   '*************************************************************************
   '     Sub: setMessage()
   '  Author: Ron Savage
   '    Date: 05/14/2009
   '
   ' This sets the text of the message.
   '*************************************************************************
   Public Sub setMessage(ByVal msgId As Integer, ByVal msgText As String)
      Dim rowsFound() As System.Data.DataRow
      Dim msgRow As System.Data.DataRow

      If (Not IsNothing(msgTable)) Then
         rowsFound = msgTable.Select("Id = " + msgId.ToString)

         If (rowsFound.Length > 0) Then
            msgRow = rowsFound(0)

            msgRow.Item("Id") = msgId
            msgRow.Item("Text") = msgText
         Else
            msgRow = msgTable.NewRow()

            msgRow.Item("Id") = msgId
            msgRow.Item("Text") = msgText

            msgTable.Rows.Add(msgRow)
         End If
      End If

      Save()
   End Sub
End Class
Ron Savage