首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > VB Dotnet >

哪位高手能帮小弟我写一个VB.NET 读取XML的例子

2012-03-20 
谁能帮我写一个VB.NET 读取XML的例子希望高手能给个例题让我学习要求VB.NET能读取,删除,修改XML中的元素中

谁能帮我写一个VB.NET 读取XML的例子
希望高手能给个例题让我学习

要求   VB.NET能读取,删除,修改XML中的元素中数据,属性中的数据,
VB.NET的源文件
XML文件

谢谢了

[解决办法]
Imports System.xml
Imports System.Collections

Public Module Mod_Main

Public Setting As New Hashtable

Sub main()
If LoadSetting() Then
Application.Run(Frm_Intro)
'Application.Run(Form1)
End If
End Sub

Private Function LoadSetting(Optional ByVal DefaultSetting As Boolean = False) As Boolean
Dim XmlDoc As New XmlDocument
Dim XmlNode As XmlNode

Try
If DefaultSetting Then
XmlDoc.LoadXml(My.Resources.DefaultSetting.ToString)
Else
XmlDoc.Load( "setting.xml ")
End If
XmlNode = XmlDoc.SelectSingleNode( "//PlaneGame ")
Setting.Add( "GameVersion ", XmlNode.Attributes( "Version ").InnerText.ToString) '版本信息
XmlNode = XmlDoc.SelectSingleNode( "//PlaneGame//PortSetting ")
For Each Xml As XmlElement In XmlNode
Dim tempstr As String()
tempstr = Xml.InnerText.Split( "- ")
If tempstr.Length > 1 Then
Setting.Add(Xml.Name & "Min ", CType(tempstr(0), Integer))
Setting.Add(Xml.Name & "Max ", CType(tempstr(1), Integer))
Else
Setting.Add(Xml.Name, tempstr(0))
End If
Next
'For Each aa As DictionaryEntry In Setting
' Console.WriteLine(aa.Key & ": " & aa.Value)
'Next
Return True
Catch ex As Exception
If MessageBox.Show( "配置文件丢失或无效,是否使用默认值代替! ", "警告 ", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button2) = DialogResult.Yes Then
GetDefaultSetting()
Return True
Else
MessageBox.Show( "配置文件加载失败,用户退出! ", "错误 ", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return False
End If
End Try

End Function

Private Sub GetDefaultSetting()
LoadSetting(True)
If MessageBox.Show( "已使用默认值代替!是否重新生成配置文件 ", "信息 ", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = DialogResult.Yes Then WriteNewSettingFile()
End Sub

Private Sub WriteNewSettingFile()
Dim XmlDoc As New XmlDocument
Try
XmlDoc.LoadXml(My.Resources.DefaultSetting.ToString)
XmlDoc.Save( "Setting.xml ")
Catch ex As Exception
MessageBox.Show(ex.Message.ToString, "错误 ", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub

End Module

热点排行