Imports System
Imports System.IO
Imports System.Xml
Imports System.Collections.Generic
Imports System.Linq
Imports System.Xml.Linq
'*********VB2010**************
Module Module1
Public Structure ServerInfo
Public 服务器名 As String
Public 表名 As String
Public 用户名 As String
Public 密码 As String
End Structure
Sub Main()
Dim Myfun As New SysInfo
Dim Use As New ServerInfo
Use = Myfun.ReadServerInfo() '读数据
Use.密码 = "sss"
Use.用户名 = "sss"
Myfun.UpdateServerInfo(Use) '更新数据
End Sub
End Module
Public Class SysInfo
Dim XmlFile As String = "..\..\SysInfo.xml"
Dim XmlXele As XElement = Nothing
Sub New()
LoadFile(XmlFile)
End Sub
Sub New(ByVal pFile As String)
XmlFile = pFile
LoadFile(pFile)
End Sub
Friend Sub LoadFile(ByVal pFile As String)
XmlXele = XElement.Load(pFile)
End Sub
Friend Sub Save()
XmlXele.Save(XmlFile)
End Sub
''' <summary>
''' 读节点内容
''' </summary>
''' <returns>结构</returns>
''' <remarks></remarks>
Friend Function ReadServerInfo() As ServerInfo
Dim pServerInfo As New ServerInfo
Dim pXele As XElement = XmlXele.Element("ServerInfo")
With pServerInfo
.服务器名 = pXele.Element("ServerName").Value
.表名 = pXele.Element("TableName").Value
.用户名 = pXele.Element("UserID").Value
.密码 = pXele.Element("PassWord").Value
End With
Return pServerInfo
End Function
''' <summary>
''' 更新节点内容
''' </summary>
''' <param name="pVal">结构参数</param>
''' <returns>布尔</returns>
''' <remarks>天力科技 </remarks>
Friend Function UpdateServerInfo(ByVal pVal As ServerInfo) As Boolean
Dim Xele = From name In XmlXele.Elements("ServerInfo") Select name
Try
For Each m As XElement In Xele
m.Element("ServerName").Value = pVal.服务器名
m.Element("TableName").Value = pVal.表名
m.Element("UserID").Value = pVal.用户名
m.Element("PassWord").Value = pVal.密码
Next
Save()
Return True
Catch ex As Exception
MsgBox("更新出错!")
Return False
End Try
End Function
End Class