這不完全是你問的,但我發現處理原始XML會導致很多頭痛。您可能會考慮的僅僅是處理一個Shift類,它允許您在登錄/註銷時執行邏輯,並讓.NET爲您執行序列化/反序列化。
這樣,如果您的業務對象和關係發生變化,您就不會綁定到特定的XML路徑。
再次,不是你問了什麼,但是這裏是我將如何解決你正在處理的商業案例。
首先,在這裏創建一個移位類,我可以把業務邏輯在簡單的例子:
Public Class Shift
Public Property Name As String
Public Property DateString As String
Public Property Login As String
Public Property Logout As String
End Class
接下來,創建轉變的集合。我稱這個類爲TimeCollection,但可以隨意調用它。將它標記爲Serializable,以便.NET可以將它從對象轉換爲XML,反之亦然。
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.IO
Imports System.Xml.Serialization
<Serializable()> Public Class TimeCollection
Public Property Path As String
<XmlArray("Shifts")>
<XmlArrayItem(GetType(Shift))>
Public Property Shift As Shift()
Public Function Serialize(FileInfo As System.IO.FileInfo)
Try
If File.Exists(FileInfo.FullName) Then
File.Delete(FileInfo.FullName)
End If
If Not Directory.Exists(FileInfo.DirectoryName) Then
Directory.CreateDirectory(FileInfo.DirectoryName)
End If
Me.Path = FileInfo.FullName
Dim serializer As XmlSerializer = New XmlSerializer(GetType(TimeCollection))
Dim writer As StreamWriter = New StreamWriter(FileInfo.FullName)
serializer.Serialize(writer, Me)
writer.Close()
Catch ex As Exception
Throw
End Try
End Function
Public Shared Function Deserialize(FileInfo As FileInfo) As TimeCollection
Dim serializedType As TimeCollection = Nothing
Dim path As String = FileInfo.FullName
If (Not File.Exists(path)) Then
Deserialize = serializedType
Else
Try
Dim serializer As XmlSerializer = New XmlSerializer(GetType(TimeCollection))
Dim reader As StreamReader = New StreamReader(path)
serializedType = serializer.Deserialize(reader)
reader.Close()
Deserialize = serializedType
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
End If
End Function
End Class
現在。如果你有一些代碼,生成一系列的變化的,就像這樣:
Dim tc As TimeCollection = New TimeCollection()
Dim first As Shift = New Shift()
first.Name = "Philipp"
first.Login = "14:11"
first.Logout = "14:45"
first.DateString = "3/31/2013"
Dim second As Shift = New Shift()
second.Name = "Phillip"
second.Login = "14:09"
' second.Logout = "15:01" (note 2nd shift has no logout)
second.DateString = "4/1/2013"
tc.Shift = New Shift(1) {first, second}
您可以輕鬆地序列化TimeCollection對象,像這樣:
tc.Serialize(New FileInfo("C:\SomePath\TimeCollectionA.xml"))
它創建了以下內容:
<?xml version="1.0" encoding="utf-8"?>
<TimeCollection xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Path>C:\Temp\Tc.xml</Path>
<Shifts>
<Shift>
<Name>Philipp</Name>
<DateString>3/31/2013</DateString>
<Login>14:11</Login>
<Logout>14:45</Logout>
</Shift>
<Shift>
<Name>Phillip</Name>
<DateString>4/1/2013</DateString>
<Login>14:09</Login>
</Shift>
</Shifts>
</TimeCollection>
然後,要反序列化內容並將文件重新轉換爲對象集合,您可以採取以下操作:
Dim tc As TimeCollection
tc = TimeCollection.Deserialize(New FileInfo("C:\SomePath\TimeCollectionA.xml"))
現在您可以遍歷tc.Shift數組等等
我將標記從「vb6」更改爲「vb.net」。 – 2013-03-29 00:46:48
我對GojiraDeMonstah的回答+1了。如果還不算太晚,如果只有不到1000行的代碼執行此XML Run,RUN,RUNNNN的速度可以儘可能快地遠離直接操作XML並使用對象運行TO。我無法強調能夠以1/3的錯誤快速編寫3x的精彩程度。我可以說,相信我,你會喜歡它的。 – FastAl 2013-04-02 20:12:43
我錯了,還是'InnerText'應該只是'Text' – Luc125 2013-04-02 23:22:08