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

请教哪位大侠有VB.net操纵XML的完整例子

2012-02-24 
请问哪位大侠有VB.net操纵XML的完整例子?发份给我好么,wubin264@163.com大致有:1、创建、修改、删除节点、属性

请问哪位大侠有VB.net操纵XML的完整例子?
发份给我好么,wubin264@163.com
大致有:
1、创建、修改、删除节点、属性;
2、遍历指定的节点;
。。。

[解决办法]
搜索一下看看。
[解决办法]
我给你一个类试试看吧。

Imports System.Xml

Public Class XmlConfig

Dim XmlFile As String = "AppConfig.xml"
Dim MyXmlDoc As New XmlDocument
Dim Root As XmlNode

Public Sub New(Optional ByVal TheXmlFile As String = "")
If TheXmlFile <> "" Then XmlFile = TheXmlFile
If Not System.IO.File.Exists(XmlFile) Then '若XML配置文件不存在,则创建
Dim MyXmlWrite As New XmlTextWriter(XmlFile, Nothing)
MyXmlWrite.WriteStartDocument()
MyXmlWrite.Formatting = Formatting.Indented
MyXmlWrite.WriteStartElement("The-Application-Settings")
MyXmlWrite.WriteAttributeString("名称", "DotNet 2005 Style Explorer")
MyXmlWrite.WriteAttributeString("作者", "123")
MyXmlWrite.WriteElementString("版权所有", "456")
MyXmlWrite.WriteElementString("注意", "该文件为系统运行的配置文件,请不要手动修改,否则后果自负!")
MyXmlWrite.WriteEndElement()
MyXmlWrite.WriteEndDocument()
MyXmlWrite.Close()
End If
MyXmlDoc.Load(XmlFile) '加载配置文件
Root = MyXmlDoc.SelectSingleNode("The-Application-Settings") '根节点
End Sub

Public Function GetElement(ByVal node As String, ByVal element As String, Optional ByVal DefaultValue As String = "") As String
Try
Dim TheNode As XmlElement = Root.SelectSingleNode(node)
If TheNode IsNot Nothing Then
Dim Child As XmlNode = TheNode.SelectSingleNode(element)
If Child IsNot Nothing Then Return Child.InnerText
End If
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "应用程序配置文件错误")
End Try

Return DefaultValue
End Function

Public Sub SaveElement(ByVal node As String, ByVal element As String, ByVal value As String)
Try
Dim TheNode As XmlElement = Root.SelectSingleNode(node)
If TheNode Is Nothing Then TheNode = MyXmlDoc.CreateElement(node)
Dim Child As XmlElement = TheNode.SelectSingleNode(element)
If Child Is Nothing Then Child = MyXmlDoc.CreateElement(element)
Child.InnerText = value
TheNode.AppendChild(Child)
Root.AppendChild(TheNode)
MyXmlDoc.Save(XmlFile)
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "应用程序配置文件错误")
End Try
End Sub

Public Sub DeleteElement(ByVal node As String, ByVal element As String)
Try
Dim TheNode As XmlElement = Root.SelectSingleNode(node)
If TheNode IsNot Nothing Then
Dim Child As XmlElement = TheNode.SelectSingleNode(element)
If Child IsNot Nothing Then
TheNode.RemoveChild(Child)
MyXmlDoc.Save(XmlFile)
End If
End If
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "应用程序配置文件错误")
End Try
End Sub

End Class

[解决办法]
使用VB9吧,操作XML文档易如反掌,根本不需要任何类.
安装一下VS2008,然后阅读MSDN的关于XML语言集成与XML LINQ部分.
[解决办法]
帮你顶,学习
[解决办法]
推荐本书电子档的.<VB 高级程序员指南>里面讲的挺详细.
[解决办法]
下面是我以前做的程序中的代码,可以参考一下:
删除xml文件中的指定元素:

VB.NET code
    Private Sub DeleteLifeArticle(ByVal title As String, ByRef isDeleted As Boolean)        '参数title为要删除的指定元素        isDeleted = False        Dim fileXML As String = Me.ArticlLifeDir + "life.xml"        Dim doc As New Xml.XmlDocument        Try            doc.Load(fileXML)            Dim list As Xml.XmlNodeList = doc.SelectSingleNode("Articles").ChildNodes            For Each node As Xml.XmlNode In list                If node.NodeType = XmlNodeType.Element Then                    If node.Attributes("Title").Value = title Then                        node.ParentNode.RemoveChild(node)                        isDeleted = True                        doc.Save(fileXML)                        Exit For                    End If                End If            Next        Catch ex As Exception            MessageBox.Show(ex.Message)        End Try    End Sub 

热点排行