XML类
Public Class XMLHelper
Dim mXmlDoc As New System.Xml.XmlDocument
Public XmlFile As String
Public Sub New(ByVal File As String)
MyClass.XmlFile = File
MyClass.mXmlDoc.Load(MyClass.XmlFile) ‘加载配置文件
End Sub
‘功能:取得元素值
‘参数:node--节点 element--元素名
‘返回:元素值 字符型
‘ $--表示出错误
Public Function GetElement(ByVal node As String, ByVal element As String) As String
On Error GoTo Err
Dim mXmlNode As System.Xml.XmlNode = mXmlDoc.SelectSingleNode("//" + node)
‘读数据
Dim xmlNode As System.Xml.XmlNode = mXmlNode.SelectSingleNode(element)
Return xmlNode.InnerText.ToString
Err:
Return "$"
End Function
‘
‘功能:保存元素值
‘参数:node--节点名称 element--元素名 val--值
‘返回:True--保存成功 False--保存失败
Public Function SaveElement(ByVal node As String, ByVal element As String, ByVal val As String) As Boolean
On Error GoTo err
Dim mXmlNode As System.Xml.XmlNode = mXmlDoc.SelectSingleNode("//" + node)
Dim xmlNodeNew As System.Xml.XmlNode
xmlNodeNew = mXmlNode.SelectSingleNode(element)
xmlNodeNew.InnerText = val
mXmlDoc.Save(MyClass.XmlFile)
Return True
err:
Return False
End Function
End Class
XML文档
<?xml version="1.0" encoding="utf-8" ?>
<Config>
<Basic>
<MC>T00-0</MC>
<Date>20190908</Date>
<State>True</State>
</Basic>
</Config>
XML使用
Dim xmlfile As XMLHelper = New XMLHelper(Application.StartupPath & "\Config.xml")
读 txt_mcname.Text = xmlfile.GetElement("Basic", "MC")
写xmlfile.SaveElement("Basic", "Date", tbname)
原文:https://www.cnblogs.com/revgin/p/12745128.html