XmlNodeReader.LocalName 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得目前節點的區域名稱。
public:
virtual property System::String ^ LocalName { System::String ^ get(); };
public override string LocalName { get; }
member this.LocalName : string
Public Overrides ReadOnly Property LocalName As String
屬性值
目前節點名稱的前置詞被移除。 例如, LocalName
是 book
針對 專案 <bk:book>
。
對於沒有名稱的節點類型 (如 Text
、Comment
等),這個屬性會傳回 String.Empty。
範例
下列範例會顯示每個節點的本機名稱,如果存在,則會顯示前置詞和命名空間 URI。
#using <System.Xml.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
XmlNodeReader^ reader = nullptr;
try
{
//Create and load the XML document.
XmlDocument^ doc = gcnew XmlDocument;
doc->LoadXml( "<book xmlns:bk='urn:samples'> "
"<title>Pride And Prejudice</title>"
"<bk:genre>novel</bk:genre>"
"</book>" );
//Load the XmlNodeReader
reader = gcnew XmlNodeReader( doc );
//Parse the XML. 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 {0}", reader->NamespaceURI );
}
}
}
}
finally
{
if ( reader != nullptr )
reader->Close();
}
}
using System;
using System.IO;
using System.Xml;
public class Sample
{
public static void Main()
{
XmlNodeReader reader = null;
try
{
//Create and load the XML document.
XmlDocument doc = new XmlDocument();
doc.LoadXml("<book xmlns:bk='urn:samples'> " +
"<title>Pride And Prejudice</title>" +
"<bk:genre>novel</bk:genre>" +
"</book>");
//Load the XmlNodeReader
reader = new XmlNodeReader(doc);
//Parse the XML. 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 XmlNodeReader = Nothing
Try
'Create and load the XML document.
Dim doc As New XmlDocument()
doc.LoadXml("<book xmlns:bk='urn:samples'> " & _
"<title>Pride And Prejudice</title>" & _
"<bk:genre>novel</bk:genre>" & _
"</book>")
'Load the XmlNodeReader
reader = New XmlNodeReader(doc)
'Parse the XML. 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
備註
注意
在 .NET Framework 2.0 中,建議的做法是使用 XmlReaderSettings 類別和 Create 方法建立 XmlReader 實例。 這可讓您充分利用.NET Framework中引進的所有新功能。 如需詳細資訊,請參閱參考頁面中的 XmlReader 一節。