如何:声明和使用 XML 命名空间前缀

更新:2007 年 11 月

此示例演示如何导入 XML 命名空间前缀 ns 并在 XML 文本和 XML 轴属性中使用它。

示例

' Place Imports statements at the top of your program.  
Imports <xmlns:ns="http://SomeNamespace">

Module Sample1

    Sub SampleTransform()

        ' Create test by using a global XML namespace prefix. 

        Dim contact = _
            <ns:contact>
                <ns:name>Patrick Hines</ns:name>
                <ns:phone ns:type="home">206-555-0144</ns:phone>
                <ns:phone ns:type="work">425-555-0145</ns:phone>
            </ns:contact>

        Dim phoneTypes = _
          <phoneTypes>
              <%= From phone In contact.<ns:phone> _
                  Select <type><%= phone.@ns:type %></type> _
              %>
          </phoneTypes>

        Console.WriteLine(phoneTypes)
    End Sub

End Module

编译代码

此示例需要:

请参见

参考

Imports 语句(XML 命名空间)

XML 属性 (Attribute) 轴属性 (Property)

XML 值属性

XElement.Attributes

其他资源

在 Visual Basic 中访问 XML

Visual Basic 中的 XML