首页 > 其他 > 详细

XML文档读取

时间:2020-04-21 17:16:14      阅读:63      评论:0      收藏:0      [点我收藏+]

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)

XML文档读取

原文:https://www.cnblogs.com/revgin/p/12745128.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!