vb.net LINQ TO XML读写XML
1
Imports System.Linq2
Imports System.Xml.Linq3

4

Public Class xmlDemoClass xmlDemo5

Public Shared Sub CreateXml()Sub CreateXml()6
Dim ld As New List(Of Object)7
ld.Add(New With {.ID = "1", .Name = "人事"})8
ld.Add(New With {.ID = "2", .Name = "业务"})9

10

11
Dim lp As New List(Of Object)12
lp.Add(New With {.ID = "1", .Name = "jim", .DID = "1"})13
lp.Add(New With {.ID = "2", .Name = "tony", .DID = "2"})14
lp.Add(New With {.ID = "3", .Name = "jesson", .DID = "2"})15

16

17
Dim person = <persons>18
<%= From p In lp Where p.ID _19
Select <person>20
<ID><%= p.ID %></ID>21
<Name><%= p.Name %></Name>22
</person> %>23

24
</persons>25
person.Save("C:\persons.xml")26
Process.Start("C:\persons.xml")27

28
Dim departMent = <department><%= From d In ld _29
Select <depinfo>30
<id><%= d.id %></id>31
<name><%= d.name %></name>32
<persons>33
<%= From p In lp Where p.did = d.id _34
Select <person>35
<id><%= p.id %></id>36
<name><%= p.name %></name>37
</person> %>38
</persons>39
</depinfo> %>40
</department>41
departMent.Save("c:\departMent.xml")42
Process.Start("c:\departMent.xml")43

44
45
End Sub46

Public Shared Sub readXML()Sub readXML()47
48
Dim xdoc = XDocument.Load("C:\persons.xml")49
Dim ps = From p In xdoc.Descendants("person") _50
Select New With {.ID = p.Element("ID").Value, .Name = p.Element("Name").Value}51
For Each p In ps52
MsgBox(p.ID & Space(3) & p.Name)53
Next54
End Sub55
End Class56

57

浙公网安备 33010602011771号