自我介绍
我是51CTO新人,初来学习,喜欢研究计算机,学习dotnet的winform,望各位前辈老师们多多指教
技术分享
今天带来的是学习XML文件的读写操作,随便写的,不过方法因该是通用的。
'引用
Imports System.Xml
'使用方法
MsgBox(XmlFileWriter($"{Application.StartupPath}\PersonnelInfo.xml", True, TextBox6.Text, ComboBox3.Text, NumericUpDown1.Value, TextBox7.Text, TextBox8.Text, TextBox9.Text))
TextBox10.Text = XmlFileReader($"{Application.StartupPath}\PersonnelInfo.xml")
'绑定
Dim vinfo As New XMLFileBinding($"{Application.StartupPath}\PersonnelInfo.xml")
TextBox6.Text = vinfo.Name
Select Case vinfo.Gender
Case "男"
ComboBox3.SelectedIndex = 0
Case "女"
ComboBox3.SelectedIndex = 1
End Select
NumericUpDown1.Value = CInt(vinfo.Age)
TextBox7.Text = vinfo.Position
TextBox8.Text = vinfo.Ability
TextBox9.Text = vinfo.Note
'实现方法
''' <summary>
''' 根据指定的文件名称(包含完整的路径)及相应的参数生成XML文档
''' </summary>
''' <param name="vFileName">指定要生成的XML文件,包含完整路径</param>
''' <param name="vOverWrite">指定一个布尔值表示是否覆盖相同的文件</param>
''' <param name="vTheName">指定名称参数</param>
''' <param name="vGender">指定性别参数</param>
''' <param name="vAge">指定年龄参数</param>
''' <param name="vPosition">指定职位参数</param>
''' <param name="vAbility">指定能力参数</param>
''' <param name="vNote">指定注释参数</param>
''' <returns>返回一个表示执行结果的字符串,据此判断是否正确执行</returns>
''' <remarks>本方法可通过一个完整路径的文件名称生成对应的xml文档,该文档的一些内容参数可根据需要进行修改调整。</remarks>
Private Function XmlFileWriter(vFileName As String, vOverWrite As Boolean, vTheName As Object, vGender As Object, vAge As Object, vPosition As Object, vAbility As Object, vNote As Object) As String
Dim result As String = "",
xmlWS As New XmlWriterSettings With {
.Indent = True, '是否打开字符缩进
.NewLineOnAttributes = False '是否打开在新的一行写入属性
}
If Not vOverWrite AndAlso File.Exists(vFileName) Then result = "vFileName 所指定的文件已存在"
If Len(result) = 0 AndAlso Path.GetExtension(vFileName) <> ".xml" Then result = "vFileName 所指定的扩展名有误"
If Len(result) = 0 AndAlso Not Directory.Exists(Path.GetDirectoryName(vFileName)) Then result = "vFileName 所指定的文件夹不存在"
If Len(result) = 0 AndAlso Len(vTheName) < 1 Then result = "vTheName 所指定的名称不能为空"
If Len(result) = 0 AndAlso IsNumeric(vTheName.ToString.Substring(0, 1)) Then result = "vTheName 所指定的名称首位不能是数字"
If Len(result) = 0 Then
Using xmlW As XmlWriter = XmlWriter.Create(vFileName, xmlWS)
Try
With xmlW
.WriteComment("这是一个人员信息测试XML文档") '写入注释
.WriteStartElement($"人员信息") '必须先写入开始元素(名称),并且与最后的WriteEndElement结束元素配套,注意,该元素名称第一个字符不能是数字。
'.WriteAttributeString("称号", "无极剑圣") '写入一些属性
.WriteElementString("姓名", $"{vTheName}") '写入一些名称和值
.WriteElementString("性别", $"{vGender}")
.WriteElementString("年龄", $"{vAge}")
.WriteElementString("职位", $"{vPosition}")
.WriteElementString("技能", $"{vAbility}")
.WriteElementString("描述", $"{vNote}")
.WriteEndElement()
.Flush() '刷新流文档
End With
result = "Complete"
Catch ex As Exception
result = ex.Message
End Try
End Using
End If
Return result
End Function
''' <summary>
''' 根据指定的XML文件名称(包含完整的路径)来读取配置信息
''' </summary>
''' <param name="vFileName">指定要读取的XML文件名称,应该包含完整路径</param>
''' <returns>返回一个表示执行结果的字符串,如果一切顺利字符串将包含配置信息</returns>
''' <remarks>根据指定的XML文件名称读取相应的配置信息,XML文件名称应包含完整的路径,并且返回一个包含执行结果或者正确信息的字符串。</remarks>
Private Function XmlFileReader(vFileName As String) As String
Dim result As String = ""
Dim xmlRS As New XmlReaderSettings With {
.IgnoreComments = True, '是否忽略注释
.IgnoreWhitespace = True,' 是否忽略空白符
.IgnoreProcessingInstructions = True '是否忽略处理指令
}
If File.Exists(vFileName) Then
Using xmlR As XmlReader = XmlReader.Create(vFileName, xmlRS)
Try
While xmlR.Read
Select Case xmlR.NodeType
Case XmlNodeType.XmlDeclaration
Case XmlNodeType.Element
Select Case xmlR.Name
Case "人员信息"
result += $"{xmlR.Name}"
Case Else
result += $"{vbCrLf}{xmlR.Name}:"
End Select
Case XmlNodeType.Text
result += $"{xmlR.Value}"
Case XmlNodeType.EndElement
Case Else
End Select
End While
Catch ex As Exception
result = ex.Message
End Try
End Using
Else
result = "vFileName 所指定的文件不存在"
End If
Return result
End Function
''' <summary>
''' 获取XML绑定的数据值
''' </summary>
Private Structure XMLFileBinding
Dim _name, _gender, _age, _position, _ability, _note As String
Public Sub New(sfile As String)
Dim xmlRS As New XmlReaderSettings With {
.IgnoreComments = True, '是否忽略注释
.IgnoreWhitespace = True,' 是否忽略空白符
.IgnoreProcessingInstructions = True '是否忽略处理指令
}
If File.Exists(sfile) Then
Using xmlR As XmlReader = XmlReader.Create(sfile, xmlRS)
Try
While xmlR.Read
Select Case xmlR.NodeType
Case XmlNodeType.Element
Select Case xmlR.Name
Case "姓名"
xmlR.Read()
_name = $"{xmlR.Value}"
Case "性别"
xmlR.Read()
_gender = $"{xmlR.Value}"
Case "年龄"
xmlR.Read()
_age = $"{xmlR.Value}"
Case "职位"
xmlR.Read()
_position = $"{xmlR.Value}"
Case "技能"
xmlR.Read()
_ability = $"{xmlR.Value}"
Case "描述"
xmlR.Read()
_note = $"{xmlR.Value}"
Case Else
End Select
Case Else
End Select
End While
Catch ex As Exception
_name = "无"
_gender = "男"
_age = "0"
_position = "无"
_ability = "无"
_note = "无"
End Try
End Using
Else
_name = "无"
_gender = "男"
_age = "0"
_position = "无"
_ability = "无"
_note = "无"
End If
End Sub
Public ReadOnly Property Name As String
Get
Return _name
End Get
End Property
Public ReadOnly Property Gender As String
Get
Return _gender
End Get
End Property
Public ReadOnly Property Age As String
Get
Return _age
End Get
End Property
Public ReadOnly Property Position As String
Get
Return _position
End Get
End Property
Public ReadOnly Property Ability As String
Get
Return _ability
End Get
End Property
Public ReadOnly Property Note As String
Get
Return _note
End Get
End Property
End Structure
结束语
技术和经验有限,哪儿不合适的请各位前辈和大佬批评指正。