代码
<?xml version="1.0" encoding="utf-8" ?>
<root>
<!--当前酒店信息-->
<CurretnHotelInfo>
<HotelId>-1</HotelId>
<SmtpServer>222.22.2.222</SmtpServer>
<EmailBcc>aaaa@aaa.com</EmailBcc>
</CurretnHotelInfo>
<HotelInfoList>
<HotelItem id="1" version="20100206" />
<HotelItem id="2" version="20100206" />
<HotelItem id="3" version="20100205" />
<HotelItem id="4" version="20100206" />
<HotelItem id="5" version="20100206" />
<HotelItem id="6" version="20100206" />
<HotelItem id="7" version="20100206" />
<HotelItem id="8" version="20100206" />
</HotelInfoList>
</root>
<root>
<!--当前酒店信息-->
<CurretnHotelInfo>
<HotelId>-1</HotelId>
<SmtpServer>222.22.2.222</SmtpServer>
<EmailBcc>aaaa@aaa.com</EmailBcc>
</CurretnHotelInfo>
<HotelInfoList>
<HotelItem id="1" version="20100206" />
<HotelItem id="2" version="20100206" />
<HotelItem id="3" version="20100205" />
<HotelItem id="4" version="20100206" />
<HotelItem id="5" version="20100206" />
<HotelItem id="6" version="20100206" />
<HotelItem id="7" version="20100206" />
<HotelItem id="8" version="20100206" />
</HotelInfoList>
</root>
代码
Imports System.Configuration
Imports System.Xml
Public Class HotelInfoConfigManager
Private Const HotelIdKeyName As String = "HotelId"
Private Const VersionKeyName As String = "Version"
Private Shared _xmlFileName As String = String.Concat(Application.StartupPath + "\HotelInfoConfig.xml")
''' <summary>
''' 读取配置文件中的当前酒店Id
''' </summary>
''' <returns></returns>
''' <remarks></remarks>
Public Shared Function GetHotelId() As String
Dim xmldoc As New Xml.XmlDocument()
Try
xmldoc.Load(_xmlFileName)
Dim node As XmlNode = xmldoc.GetElementsByTagName("CurretnHotelInfo")(0)
Dim result As String = node.ChildNodes(0).InnerText
Return result
Catch ex As Exception
Throw New Exception("读取配置文件中的当前酒店Id异常!")
End Try
End Function
''' <summary>
''' 设置酒店ID到配置文件中
''' </summary>
''' <param name="hotelId"></param>
''' <remarks></remarks>
Public Shared Function SetHotelId(ByVal hotelId As String) As Boolean
Dim xmldoc As New Xml.XmlDocument()
Try
xmldoc.Load(_xmlFileName)
Dim node As XmlNode = xmldoc.GetElementsByTagName("CurretnHotelInfo")(0)
node.ChildNodes(0).InnerText = hotelId
xmldoc.Save(_xmlFileName)
Return True
Catch ex As Exception
Return False
End Try
End Function
''' <summary>
''' 根据酒店Id读取对应的版本号
''' </summary>
''' <param name="hotelId"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Shared Function GetVersionByHotelId(ByVal hotelId As String) As String
Dim xmldoc As New Xml.XmlDocument()
Dim result As String = String.Empty
Try
xmldoc.Load(_xmlFileName)
Dim listNode As XmlNode = xmldoc.GetElementsByTagName("HotelInfoList")(0)
For Each item As XmlNode In listNode.ChildNodes
If item.Attributes("id").Value = hotelId Then
result = item.Attributes("version").Value
Exit For
End If
Next
Return result
Catch ex As Exception
Throw New Exception("根据酒店Id读取对应的版本号出现异常!")
End Try
End Function
''' <summary>
''' 读取配置文件中的当前酒店对应的版本号
''' </summary>
''' <returns></returns>
''' <remarks></remarks>
Public Shared Function GetVersion() As String
Dim hotelId As String = GetHotelId()
Return GetVersionByHotelId(hotelId)
End Function
''' 读取配置文件中的邮件服务器地址
''' </summary>
''' <returns></returns>
''' <remarks></remarks>
Public Shared Function GetEmailServer() As String
Dim xmldoc As New Xml.XmlDocument()
Try
xmldoc.Load(_xmlFileName)
Dim node As XmlNode = xmldoc.GetElementsByTagName("CurretnHotelInfo")(0)
Dim result As String = node.ChildNodes(1).InnerText
Return result
Catch ex As Exception
Throw New Exception("读取配置文件中的邮件服务器地址异常!")
End Try
End Function
''' 读取配置文件中的邮件地址
''' </summary>
''' <returns></returns>
''' <remarks></remarks>
Public Shared Function GetBccEmail() As String
Dim xmldoc As New Xml.XmlDocument()
Try
xmldoc.Load(_xmlFileName)
Dim node As XmlNode = xmldoc.GetElementsByTagName("CurretnHotelInfo")(0)
Dim result As String = node.ChildNodes(2).InnerText
Return result
Catch ex As Exception
Throw New Exception("读取配置文件中的邮件地址异常!")
End Try
End Function
End Class
Imports System.Xml
Public Class HotelInfoConfigManager
Private Const HotelIdKeyName As String = "HotelId"
Private Const VersionKeyName As String = "Version"
Private Shared _xmlFileName As String = String.Concat(Application.StartupPath + "\HotelInfoConfig.xml")
''' <summary>
''' 读取配置文件中的当前酒店Id
''' </summary>
''' <returns></returns>
''' <remarks></remarks>
Public Shared Function GetHotelId() As String
Dim xmldoc As New Xml.XmlDocument()
Try
xmldoc.Load(_xmlFileName)
Dim node As XmlNode = xmldoc.GetElementsByTagName("CurretnHotelInfo")(0)
Dim result As String = node.ChildNodes(0).InnerText
Return result
Catch ex As Exception
Throw New Exception("读取配置文件中的当前酒店Id异常!")
End Try
End Function
''' <summary>
''' 设置酒店ID到配置文件中
''' </summary>
''' <param name="hotelId"></param>
''' <remarks></remarks>
Public Shared Function SetHotelId(ByVal hotelId As String) As Boolean
Dim xmldoc As New Xml.XmlDocument()
Try
xmldoc.Load(_xmlFileName)
Dim node As XmlNode = xmldoc.GetElementsByTagName("CurretnHotelInfo")(0)
node.ChildNodes(0).InnerText = hotelId
xmldoc.Save(_xmlFileName)
Return True
Catch ex As Exception
Return False
End Try
End Function
''' <summary>
''' 根据酒店Id读取对应的版本号
''' </summary>
''' <param name="hotelId"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Shared Function GetVersionByHotelId(ByVal hotelId As String) As String
Dim xmldoc As New Xml.XmlDocument()
Dim result As String = String.Empty
Try
xmldoc.Load(_xmlFileName)
Dim listNode As XmlNode = xmldoc.GetElementsByTagName("HotelInfoList")(0)
For Each item As XmlNode In listNode.ChildNodes
If item.Attributes("id").Value = hotelId Then
result = item.Attributes("version").Value
Exit For
End If
Next
Return result
Catch ex As Exception
Throw New Exception("根据酒店Id读取对应的版本号出现异常!")
End Try
End Function
''' <summary>
''' 读取配置文件中的当前酒店对应的版本号
''' </summary>
''' <returns></returns>
''' <remarks></remarks>
Public Shared Function GetVersion() As String
Dim hotelId As String = GetHotelId()
Return GetVersionByHotelId(hotelId)
End Function
''' 读取配置文件中的邮件服务器地址
''' </summary>
''' <returns></returns>
''' <remarks></remarks>
Public Shared Function GetEmailServer() As String
Dim xmldoc As New Xml.XmlDocument()
Try
xmldoc.Load(_xmlFileName)
Dim node As XmlNode = xmldoc.GetElementsByTagName("CurretnHotelInfo")(0)
Dim result As String = node.ChildNodes(1).InnerText
Return result
Catch ex As Exception
Throw New Exception("读取配置文件中的邮件服务器地址异常!")
End Try
End Function
''' 读取配置文件中的邮件地址
''' </summary>
''' <returns></returns>
''' <remarks></remarks>
Public Shared Function GetBccEmail() As String
Dim xmldoc As New Xml.XmlDocument()
Try
xmldoc.Load(_xmlFileName)
Dim node As XmlNode = xmldoc.GetElementsByTagName("CurretnHotelInfo")(0)
Dim result As String = node.ChildNodes(2).InnerText
Return result
Catch ex As Exception
Throw New Exception("读取配置文件中的邮件地址异常!")
End Try
End Function
End Class