XmlTextReader.NamespaceURI 屬性

定義

取得讀取器所在節點的命名空間 URI(依據 W3C 命名空間規範定義)。

public:
 virtual property System::String ^ NamespaceURI { System::String ^ get(); };
public override string NamespaceURI { get; }
member this.NamespaceURI : string
Public Overrides ReadOnly Property NamespaceURI As String

屬性值

目前節點的命名空間 URI;否則則為空字串。

範例

以下範例顯示每個節點的本地名稱,若存在則顯示前綴與命名空間 URI。

using System;
using System.IO;
using System.Xml;

public class Sample {

  public static void Main() {

    XmlTextReader reader = null;

    try {

       // Load the reader with the XML file.
       reader = new XmlTextReader("book2.xml");

       // Parse the file.  If they exist, display the prefix and
       // namespace URI of each node.
       while (reader.Read()) {
         if (reader.IsStartElement()) {
           if (reader.Prefix==String.Empty)
                    {
                        Console.WriteLine("<{0}>", reader.LocalName);
                    }
                    else {
               Console.Write("<{0}:{1}>", reader.Prefix, reader.LocalName);
               Console.WriteLine(" The namespace URI is " + reader.NamespaceURI);
           }
         }
       }
     }
     finally {
        if (reader != null)
          reader.Close();
      }
  }
} // End class
Imports System.IO
Imports System.Xml

Public Class Sample
    
    Public Shared Sub Main()
        Dim reader As XmlTextReader = Nothing
        
        Try
            ' Load the reader with the XML file.
            reader = New XmlTextReader("book2.xml")
            
            ' Parse the file.  If they exist, display the prefix and 
            ' namespace URI of each node.
            While reader.Read()
                If reader.IsStartElement() Then
                    If reader.Prefix = String.Empty Then
                        Console.WriteLine("<{0}>", reader.LocalName)
                    Else
                        Console.Write("<{0}:{1}>", reader.Prefix, reader.LocalName)
                        Console.WriteLine(" The namespace URI is " & reader.NamespaceURI)
                    End If
                End If
            End While
        
        Finally
            If Not (reader Is Nothing) Then
                reader.Close()
            End If
        End Try
    End Sub
End Class

範例中使用檔案 book2.xml,作為輸入。


<book xmlns:bk='urn:samples'>
  <title>Pride And Prejudice</title>
  <bk:genre>novel</bk:genre>
</book>

備註

Note

建議您使用 XmlReader 方法來建立XmlReader.Create實例,以利用新功能。

此性質僅與節點相關ElementAttribute

適用於

另請參閱