XmlNodeReader.Name 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得目前節點的限定名稱 (Qualified Name)。
public:
virtual property System::String ^ Name { System::String ^ get(); };
public override string Name { get; }
member this.Name : string
Public Overrides ReadOnly Property Name As String
屬性值
目前節點的限定名稱。 例如, Name
是 bk:book
針對 專案 <bk:book>
。
傳回的名稱需視節點的 NodeType 而定: 下列節點類型會傳回所列的值。 其他所有節點類型都會傳回空字串。
節點類型 | 名稱 |
---|---|
Attribute | 屬性的名稱。 |
DocumentType | 文件類型名稱。 |
Element | 標記名稱。 |
EntityReference | 所參考的實體名稱。 |
ProcessingInstruction | 處理指示的目標。 |
XmlDeclaration |
xml 常值 (Literal) 字串。
|
範例
下列範例會讀取 XML 並顯示每個節點。
#using <System.Xml.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
String^ filename = "items.xml";
XmlNodeReader^ reader = nullptr;
try
{
//Create an XmlNodeReader to read the XmlDocument.
XmlDocument^ doc = gcnew XmlDocument;
doc->Load( filename );
reader = gcnew XmlNodeReader( doc );
//Parse the file and display each of the nodes.
while ( reader->Read() )
{
switch ( reader->NodeType )
{
case XmlNodeType::Element:
Console::Write( "<{0}>", reader->Name );
break;
case XmlNodeType::Text:
Console::Write( reader->Value );
break;
case XmlNodeType::CDATA:
Console::Write( reader->Value );
break;
case XmlNodeType::ProcessingInstruction:
Console::Write( "<?{0} {1}?>", reader->Name, reader->Value );
break;
case XmlNodeType::Comment:
Console::Write( "<!--{0}-->", reader->Value );
break;
case XmlNodeType::XmlDeclaration:
Console::Write( "<?xml version='1.0'?>" );
break;
case XmlNodeType::Document:
break;
case XmlNodeType::EndElement:
Console::Write( "</{0}>", reader->Name );
break;
}
}
}
finally
{
if ( reader != nullptr )
reader->Close();
}
}
using System;
using System.IO;
using System.Xml;
public class Sample
{
private const String filename = "items.xml";
public static void Main()
{
XmlNodeReader reader = null;
try
{
//Create an XmlNodeReader to read the XmlDocument.
XmlDocument doc = new XmlDocument();
doc.Load(filename);
reader = new XmlNodeReader(doc);
//Parse the file and display each of the nodes.
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element:
Console.Write("<{0}>", reader.Name);
break;
case XmlNodeType.Text:
Console.Write(reader.Value);
break;
case XmlNodeType.CDATA:
Console.Write(reader.Value);
break;
case XmlNodeType.ProcessingInstruction:
Console.Write("<?{0} {1}?>", reader.Name, reader.Value);
break;
case XmlNodeType.Comment:
Console.Write("<!--{0}-->", reader.Value);
break;
case XmlNodeType.XmlDeclaration:
Console.Write("<?xml version='1.0'?>");
break;
case XmlNodeType.Document:
break;
case XmlNodeType.EndElement:
Console.Write("</{0}>", reader.Name);
break;
}
}
}
finally
{
if (reader!=null)
reader.Close();
}
}
} // End class
Imports System.IO
Imports System.Xml
Public Class Sample
Private Const filename As String = "items.xml"
Public Shared Sub Main()
Dim reader As XmlNodeReader = Nothing
Try
'Create an XmlNodeReader to read the XmlDocument.
Dim doc As New XmlDocument()
doc.Load(filename)
reader = New XmlNodeReader(doc)
'Parse the file and display each of the nodes.
While reader.Read()
Select Case reader.NodeType
Case XmlNodeType.Element
Console.Write("<{0}>", reader.Name)
Case XmlNodeType.Text
Console.Write(reader.Value)
Case XmlNodeType.CDATA
Console.Write(reader.Value)
Case XmlNodeType.ProcessingInstruction
Console.Write("<?{0} {1}?>", reader.Name, reader.Value)
Case XmlNodeType.Comment
Console.Write("<!--{0}-->", reader.Value)
Case XmlNodeType.XmlDeclaration
Console.Write("<?xml version='1.0'?>")
Case XmlNodeType.Document
Case XmlNodeType.EndElement
Console.Write("</{0}>", reader.Name)
End Select
End While
Finally
If Not (reader Is Nothing) Then
reader.Close()
End If
End Try
End Sub
End Class
此範例會使用 檔案 items.xml
,作為輸入。
<?xml version="1.0"?>
<!-- This is a sample XML document -->
<!DOCTYPE Items [<!ENTITY number "123">]>
<Items>
<Item>Test with an entity: &number;</Item>
<Item>test with a child element <more/> stuff</Item>
<Item>test with a CDATA section <![CDATA[<456>]]> def</Item>
<Item>Test with a char entity: A</Item>
<!-- Fourteen chars in this element.-->
<Item>1234567890ABCD</Item>
</Items>
備註
注意
在 .NET Framework 2.0 中,建議的做法是使用 XmlReaderSettings 類別和 Create 方法建立 XmlReader 實例。 這可讓您充分利用.NET Framework中引進的所有新功能。 如需詳細資訊,請參閱參考頁面中的 XmlReader 一節。