Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从XML文件调用文本消息_C#_Xml_Visual Studio 2008 - Fatal编程技术网

C# 从XML文件调用文本消息

C# 从XML文件调用文本消息,c#,xml,visual-studio-2008,C#,Xml,Visual Studio 2008,目前,我正在使用Visual Studio 2008和C#开发一个使用分层体系结构的软件。 在该系统中,我希望将所有文本消息(必要时将使用消息框显示)保存在XML文件中,这样,如果我更新任何消息,就不必编译整个系统。我怎样才能做到这一点?XML文件结构是什么?如何从C#代码调用消息?请描述并举例说明。 谢谢。对于您的场景,更好的选择可能是资源卫星DLL。这是一个单独的DLL,其中包含一组动态加载到应用程序中的资源。这就是有多少不同的应用程序需要本地化。最重要的是,有许多工具支持这种类型的功能 更

目前,我正在使用Visual Studio 2008和C#开发一个使用分层体系结构的软件。 在该系统中,我希望将所有文本消息(必要时将使用消息框显示)保存在XML文件中,这样,如果我更新任何消息,就不必编译整个系统。我怎样才能做到这一点?XML文件结构是什么?如何从C#代码调用消息?请描述并举例说明。
谢谢。

对于您的场景,更好的选择可能是资源卫星DLL。这是一个单独的DLL,其中包含一组动态加载到应用程序中的资源。这就是有多少不同的应用程序需要本地化。最重要的是,有许多工具支持这种类型的功能

更改消息只需更改此DLL即可。这确实会导致编译,但只会编译资源,而不会编译系统


您可能希望将自己的自定义部分添加到app.config文件中以存储邮件。但是,这需要您在每次消息更改时更新app.config文件


我将创建一个类,该类使用.NET数据集和DataTable存储ID和消息(以及您需要的任何其他内容)

这样,类就可以使用DataSet.WriteXML()和DataSet.ReadXML()从指定的任何文件名保存/加载,而不必担心自己“手工制作”XML格式

这听起来像是一个方便的类,所以我写了一个作为“概念证明”:


我不想在app.config文件中存储消息。相反,我希望使用另一个XML文件(比如Messages.XML)来存储消息,然后您可能希望使用XML序列化将消息的XML文件转换为消息对象数组。
'*************************************************************************
'   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