MyException - 我的异常网
当前位置:我的异常网» C# » xml资料的读取 并在Treeview控件上显示

xml资料的读取 并在Treeview控件上显示(2)

www.MyException.Cn  网友分享于:2013-12-20  浏览:90次

End Property

Public ReadOnly Property XmlText() As String
Get
Return XmlDoc.InnerXml
End Get
End Property

Sub New(ByVal FileName As String, Optional ByVal CreateNew As Boolean = True, Optional ByVal Root As String = "XML ", Optional ByRef IsOK As Boolean = False)
IsOK = False
XmlFile = " "
Dim reader As System.Xml.XmlReader = Nothing
Try
reader = New System.Xml.XmlTextReader(FileName)
reader.Read()
Catch ex As Exception
If reader IsNot Nothing Then reader.Close()
Debug.Print( "New - " & ex.Message)
If Not Create(FileName, Root) Then Return
Finally
If reader IsNot Nothing Then reader.Close()
End Try

IsOK = True
XmlFile = FileName
XmlDoc = New XmlDocument
XmlDoc.Load(XmlFile)
End Sub

Public Function Create(ByVal FileName As String, Optional ByVal Root As String = "XML ") As Boolean
Dim NewXML As XmlTextWriter = Nothing
Try
NewXML = New XmlTextWriter(FileName, Nothing)
NewXML.Formatting = Formatting.Indented
NewXML.WriteStartDocument()
NewXML.WriteComment(My.Application.Info.AssemblyName & " Settings ")
NewXML.WriteStartElement(Root)
NewXML.WriteAttributeString( "Powered ", "Null ")
NewXML.WriteEndElement()
NewXML.WriteEndDocument()
NewXML.Flush()
NewXML.Close()
Catch ex As Exception
Debug.Print( "Create - " & ex.Message)
Return False
Finally
If NewXML IsNot Nothing Then
NewXML.Close()
NewXML = Nothing
End If
End Try
Return True
End Function

Public Function Save(ByVal aSection As String, ByVal aKey As String, ByVal aValue As String) As Boolean
Dim Paths() As String
Dim n As Integer
Dim Node, Node2 As XmlNode
Dim Ele As XmlElement

While Strings.Left(aSection, 1) = "/ "
aSection = Strings.Mid(aSection, 2)
End While

'段名是否为空
If aSection = " " Then
XmlDoc.DocumentElement.RemoveAll()
Else
Paths = Strings.Split(aSection, "/ ")
Try
Node = XmlDoc.DocumentElement.SelectSingleNode(Paths(n))
If Node Is Nothing Then
Ele = XmlDoc.CreateElement(Paths(n))
Node = XmlDoc.DocumentElement.AppendChild(Ele)
End If
For n = 1 To Paths.Length - 1
If Paths(n) = " " Then Continue For
Node2 = Node.SelectSingleNode(Paths(n))
If Node2 Is Nothing Then
Ele = XmlDoc.CreateElement(Paths(n))
Node2 = Node.AppendChild(Ele)
End If
Node = Node2
Next
'键名是否为空
If aKey = " " Then
Node.RemoveAll()
Else
Ele = Node.Item(aKey)
If Ele Is Nothing Then
Ele = XmlDoc.CreateElement(aKey)
Node.AppendChild(Ele)

文章评论

软件开发程序错误异常ExceptionCopyright © 2009-2015 MyException 版权所有