下面的示例演示如何访问 contacts 对象的第一个名为 name 的子代节点的值,以及所有名为 phone 的子代节点的值。
Dim contacts As XElement = _
<contacts>
<contact>
<name>Patrick Hines</name>
<phone type="home">206-555-0144</phone>
<phone type="work">425-555-0145</phone>
</contact>
</contacts>
Console.WriteLine("Name: " & contacts...<name>.Value)
Dim homePhone = From phone In contacts...<phone> _
Select phone.Value
Console.WriteLine("Home Phone = {0}", homePhone(0))
这段代码将显示以下文本:
Name: Patrick Hines
Home Phone = 206-555-0144
下面的示例将 ns 声明为 XML 命名空间前缀。然后,使用该命名空间前缀创建 XML 文本并访问第一个具有限定名 ns:name 的子节点的值。
Imports <xmlns:ns = "http://SomeNamespace">
Class TestClass2
Shared Sub TestPrefix()
Dim contacts = _
<ns:contacts>
<ns:contact>
<ns:name>Patrick Hines</ns:name>
</ns:contact>
</ns:contacts>
Console.WriteLine("Name: " & contacts...<ns:name>.Value)
End Sub
End Class
这段代码将显示以下文本:
Name: Patrick Hines