Module LinqHand2
Module LinqHand2
''' <summary>
''' 为了编写代码方便用函数产生XML文档,实际应从XML文件中读取
''' </summary>
''' <returns></returns>
''' <remarks></remarks>
Function CreateDocument()Function CreateDocument() As XDocument
Return <?xml version="1.0" encoding="UTF-16" standalone="yes"?>
<organizer>
<contacts>
<contact category="home">
<name>John Smith</name>
</contact>
<contact category="home">
<name>Sally Peters</name>
</contact>
<contact category="work">
<name>Jim Anderson</name>
</contact>
</contacts>
</organizer>
End Function
''' <summary>
''' XML文档元素查询
''' </summary>
''' <remarks></remarks>
Sub XMLQuery()Sub XMLQuery()
Dim doc = CreateDocument()
Console.WriteLine(doc)
Dim contacts = From cont In doc...<contact> Where cont.@category = "home" Select cont
Console.WriteLine(vbCrLf & vbCrLf & "Results:")
For Each contact In contacts
Console.WriteLine(vbCrLf & " {0}", contact)
Next
Dim transformedElement = <friends>
<%= From frien In contacts Select <friend fullname=<%= frien...<name>.Value %>></friend> %>
</friends>
Console.WriteLine(vbCrLf & vbCrLf & "{0}", transformedElement)
Console.ReadLine()
End Sub
Sub Main()Sub Main()
XMLQuery()
End Sub
End Module
''' <summary>
''' 为了编写代码方便用函数产生XML文档,实际应从XML文件中读取
''' </summary>
''' <returns></returns>
''' <remarks></remarks>
Function CreateDocument()Function CreateDocument() As XDocument
Return <?xml version="1.0" encoding="UTF-16" standalone="yes"?>
<organizer>
<contacts>
<contact category="home">
<name>John Smith</name>
</contact>
<contact category="home">
<name>Sally Peters</name>
</contact>
<contact category="work">
<name>Jim Anderson</name>
</contact>
</contacts>
</organizer>
End Function
''' <summary>
''' XML文档元素查询
''' </summary>
''' <remarks></remarks>
Sub XMLQuery()Sub XMLQuery()
Dim doc = CreateDocument()
Console.WriteLine(doc)
Dim contacts = From cont In doc...<contact> Where cont.@category = "home" Select cont
Console.WriteLine(vbCrLf & vbCrLf & "Results:")
For Each contact In contacts
Console.WriteLine(vbCrLf & " {0}", contact)
Next
Dim transformedElement = <friends>
<%= From frien In contacts Select <friend fullname=<%= frien...<name>.Value %>></friend> %>
</friends>
Console.WriteLine(vbCrLf & vbCrLf & "{0}", transformedElement)
Console.ReadLine()
End Sub
Sub Main()Sub Main()
XMLQuery()
End Sub
End Module