XmlTextReader 建構函式

定義

初始化 XmlTextReader 的新執行個體。

多載

XmlTextReader()

初始化 XmlTextReader 的新執行個體。

XmlTextReader(String, XmlNodeType, XmlParserContext)

使用指定的字串、XmlNodeTypeXmlParserContext,初始化 XmlTextReader 類別的新執行個體。

XmlTextReader(String, TextReader, XmlNameTable)

使用指定的 URL、XmlTextReaderTextReader,初始化 XmlNameTable 類別的新執行個體。

XmlTextReader(String, Stream, XmlNameTable)

使用指定的 URL、資料流和 XmlNameTable,初始化 XmlTextReader 類別的新執行個體。

XmlTextReader(Stream, XmlNodeType, XmlParserContext)

使用指定的資料流、XmlNodeTypeXmlParserContext,初始化 XmlTextReader 類別的新執行個體。

XmlTextReader(String, TextReader)

使用指定的 URL 和 TextReader,初始化 XmlTextReader 類別的新執行個體。

XmlTextReader(String, Stream)

使用指定的 URL 和資料流,初始化 XmlTextReader 類別的新執行個體。

XmlTextReader(String, XmlNameTable)

使用指定的檔案和 XmlNameTable,初始化 XmlTextReader 類別的新執行個體。

XmlTextReader(Stream, XmlNameTable)

使用指定的資料流和 XmlNameTable,初始化 XmlTextReader 類別的新執行個體。

XmlTextReader(XmlNameTable)

使用指定的 XmlTextReader 初始化 XmlNameTable 類別的新執行個體。

XmlTextReader(String)

使用指定的檔案,初始化 XmlTextReader 類別的新執行個體。

XmlTextReader(TextReader)

使用指定的 XmlTextReader 初始化 TextReader 類別的新執行個體。

XmlTextReader(Stream)

使用指定的資料流初始化 XmlTextReader 類別的新執行個體。

XmlTextReader(TextReader, XmlNameTable)

使用指定的 XmlTextReaderTextReader,初始化 XmlNameTable 類別的新執行個體。

備註

注意

從 .NET Framework 2.0 開始,建議您使用 XmlReader.Create 方法來建立 XmlReader 實例,以利用新功能。

XmlTextReader()

Source:
XmlTextReader.cs
Source:
XmlTextReader.cs
Source:
XmlTextReader.cs

初始化 XmlTextReader 的新執行個體。

protected:
 XmlTextReader();
protected XmlTextReader ();
Protected Sub New ()

另請參閱

適用於

XmlTextReader(String, XmlNodeType, XmlParserContext)

Source:
XmlTextReader.cs
Source:
XmlTextReader.cs
Source:
XmlTextReader.cs

使用指定的字串、XmlNodeTypeXmlParserContext,初始化 XmlTextReader 類別的新執行個體。

public:
 XmlTextReader(System::String ^ xmlFragment, System::Xml::XmlNodeType fragType, System::Xml::XmlParserContext ^ context);
public XmlTextReader (string xmlFragment, System.Xml.XmlNodeType fragType, System.Xml.XmlParserContext? context);
public XmlTextReader (string xmlFragment, System.Xml.XmlNodeType fragType, System.Xml.XmlParserContext context);
new System.Xml.XmlTextReader : string * System.Xml.XmlNodeType * System.Xml.XmlParserContext -> System.Xml.XmlTextReader
Public Sub New (xmlFragment As String, fragType As XmlNodeType, context As XmlParserContext)

參數

xmlFragment
String

包含要剖析之 XML 片段的字串。

fragType
XmlNodeType

XML 片段的 XmlNodeType。 這也會決定片段字串可包含的內容。 (請參閱下表。)

context
XmlParserContext

要剖析的 xmlFragment 所在的 XmlParserContextXmlNameTable這包括要使用的 、編碼、命名空間範圍、目前的 xml:langxml:space 範圍。

例外狀況

fragType 不是 ElementAttributeDocumentXmlNodeType

xmlFragmentnull

範例

下列範例會剖析 XML 片段。 它會使用 XmlParserContext 和 來處理 XmlNamespaceManager 命名空間解析。

#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
   
   // Create the XML fragment to be parsed.
   String^ xmlFrag = "<book> <title>Pride And Prejudice</title> <bk:genre>novel</bk:genre> </book>";
   
   // Create the XmlNamespaceManager.
   NameTable^ nt = gcnew NameTable;
   XmlNamespaceManager^ nsmgr = gcnew XmlNamespaceManager( nt );
   nsmgr->AddNamespace( "bk", "urn:sample" );
   
   // Create the XmlParserContext.
   XmlParserContext^ context = gcnew XmlParserContext( nullptr,nsmgr,nullptr,XmlSpace::None );
   
   // Create the reader. 
   XmlTextReader^ reader = gcnew XmlTextReader( xmlFrag,XmlNodeType::Element,context );
   
   // Parse the XML.  If they exist, display the prefix and  
   // namespace URI of each element.
   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 );
         }
      }
   }

   
   // Close the reader.
   reader->Close();
}
using System;
using System.IO;
using System.Xml;

public class Sample
{
  public static void Main()
  {

    //Create the XML fragment to be parsed.
    string xmlFrag ="<book> " +
                    "<title>Pride And Prejudice</title>" +
                    "<bk:genre>novel</bk:genre>" +
                    "</book>";

    //Create the XmlNamespaceManager.
    NameTable nt = new NameTable();
    XmlNamespaceManager nsmgr = new XmlNamespaceManager(nt);
    nsmgr.AddNamespace("bk", "urn:sample");

    //Create the XmlParserContext.
    XmlParserContext context = new XmlParserContext(null, nsmgr, null, XmlSpace.None);

    //Create the reader.
    XmlTextReader reader = new XmlTextReader(xmlFrag, XmlNodeType.Element, context);

    //Parse the XML.  If they exist, display the prefix and
    //namespace URI of each element.
    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);
        }
      }
    }

    //Close the reader.
    reader.Close();
  }
} // End class
Imports System.IO
Imports System.Xml

public class Sample 

  public shared sub Main()

    'Create the XML fragment to be parsed.
    Dim xmlFrag as string ="<book> " & _
                           "<title>Pride And Prejudice</title>" & _
                           "<bk:genre>novel</bk:genre>" & _
                           "</book>" 

    'Create the XmlNamespaceManager.
    Dim nt as NameTable = new NameTable()
    Dim nsmgr as XmlNamespaceManager = new XmlNamespaceManager(nt)
    nsmgr.AddNamespace("bk", "urn:sample")

    'Create the XmlParserContext.
    Dim context as XmlParserContext = new XmlParserContext(nothing, nsmgr, nothing, XmlSpace.None)

    'Create the reader. 
    Dim reader as XmlTextReader = new XmlTextReader(xmlFrag, XmlNodeType.Element, context)
  
    'Parse the XML.  If they exist, display the prefix and  
    'namespace URI of each element.
    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)
        end if 
      end if
    end while
  
    'Close the reader.
    reader.Close()     
  
  end sub
end class

備註

注意

從 .NET Framework 2.0 開始,建議您使用 XmlReader.Create 方法來建立 XmlReader 實例,以利用新功能。

此建構函式會將指定的字串剖析為 XML 片段。 如果 XML 片段是元素或屬性,您可以略過格式正確的 XML 檔根層級規則。 此建構函式可以處理從 ReadInnerXml 傳回的字串。

下表列出 的有效值 fragType ,以及讀取器如何剖析每個不同節點類型。

XmlNodeType 片段可能包含
項目 例如,任何有效的專案內容 (、元素、批註、處理指示、CDATA 區段、文字和實體參考的任何組合) 。

您也可以提供 XML 宣告。 這可讓您指定 XML 片段的編碼方式,而不需在 物件上 XmlParserContext 設定它。
屬性 屬性的值 (引號內的元件) 。
文件 完整的 XML 文件內容。 這會強制執行檔層級規則。

另請參閱

適用於

XmlTextReader(String, TextReader, XmlNameTable)

Source:
XmlTextReader.cs
Source:
XmlTextReader.cs
Source:
XmlTextReader.cs

使用指定的 URL、XmlTextReaderTextReader,初始化 XmlNameTable 類別的新執行個體。

public:
 XmlTextReader(System::String ^ url, System::IO::TextReader ^ input, System::Xml::XmlNameTable ^ nt);
public XmlTextReader (string url, System.IO.TextReader input, System.Xml.XmlNameTable nt);
new System.Xml.XmlTextReader : string * System.IO.TextReader * System.Xml.XmlNameTable -> System.Xml.XmlTextReader
Public Sub New (url As String, input As TextReader, nt As XmlNameTable)

參數

url
String

用來解析外部資源的 URL。 BaseURI 設為這個值。 如果 urlnull,則會將 BaseURI 設為 String.Empty

input
TextReader

TextReader,包含要讀取的 XML 資料。

nt
XmlNameTable

要使用的 XmlNameTable

例外狀況

nt 值為 null

備註

注意

從 .NET Framework 2.0 開始,建議您使用 XmlReader.Create 方法來建立 XmlReader 實例,以利用新功能。

它假設已將 TextReader 設定為正確的編碼方式。 這是由已在多部分 MIME 案例中從資料流程讀取某些專案的用戶端所使用。

另請參閱

適用於

XmlTextReader(String, Stream, XmlNameTable)

Source:
XmlTextReader.cs
Source:
XmlTextReader.cs
Source:
XmlTextReader.cs

使用指定的 URL、資料流和 XmlNameTable,初始化 XmlTextReader 類別的新執行個體。

public:
 XmlTextReader(System::String ^ url, System::IO::Stream ^ input, System::Xml::XmlNameTable ^ nt);
public XmlTextReader (string url, System.IO.Stream input, System.Xml.XmlNameTable nt);
new System.Xml.XmlTextReader : string * System.IO.Stream * System.Xml.XmlNameTable -> System.Xml.XmlTextReader
Public Sub New (url As String, input As Stream, nt As XmlNameTable)

參數

url
String

用來解析外部資源的 URL。 BaseURI 設為這個值。 如果 urlnull,則會將 BaseURI 設為 String.Empty

input
Stream

包含要讀取之 XML 資料的資料流。

nt
XmlNameTable

要使用的 XmlNameTable

例外狀況

inputnt 的值為 null

備註

注意

從 .NET Framework 2.0 開始,建議您使用 XmlReader.Create 方法來建立 XmlReader 實例,以利用新功能。

另請參閱

適用於

XmlTextReader(Stream, XmlNodeType, XmlParserContext)

Source:
XmlTextReader.cs
Source:
XmlTextReader.cs
Source:
XmlTextReader.cs

使用指定的資料流、XmlNodeTypeXmlParserContext,初始化 XmlTextReader 類別的新執行個體。

public:
 XmlTextReader(System::IO::Stream ^ xmlFragment, System::Xml::XmlNodeType fragType, System::Xml::XmlParserContext ^ context);
public XmlTextReader (System.IO.Stream xmlFragment, System.Xml.XmlNodeType fragType, System.Xml.XmlParserContext? context);
public XmlTextReader (System.IO.Stream xmlFragment, System.Xml.XmlNodeType fragType, System.Xml.XmlParserContext context);
new System.Xml.XmlTextReader : System.IO.Stream * System.Xml.XmlNodeType * System.Xml.XmlParserContext -> System.Xml.XmlTextReader
Public Sub New (xmlFragment As Stream, fragType As XmlNodeType, context As XmlParserContext)

參數

xmlFragment
Stream

包含要剖析之 XML 片段的資料流。

fragType
XmlNodeType

XML 片段的 XmlNodeType。 這也會決定片段可包含的內容。 (請參閱下表。)

context
XmlParserContext

要剖析的 xmlFragment 所在的 XmlParserContextXmlNameTable這包括要使用的 、編碼、命名空間範圍、目前的 xml:langxml:space 範圍。

例外狀況

fragType 不是 Element、Attribute 或 Document XmlNodeType

xmlFragmentnull

備註

注意

從 .NET Framework 2.0 開始,建議您使用 XmlReader.Create 方法來建立 XmlReader 實例,以利用新功能。

此建構函式會將指定的字串剖析為 XML 片段。 如果 XML 片段是元素或屬性,您可以略過格式正確的 XML 檔根層級規則。

下表列出 的有效值 fragType

XmlNodeType 片段可能包含
Element 例如,任何有效的專案內容 (、元素、批註、處理指示、CDATA 區段、文字和實體參考的任何組合) 。

您也可以提供 XML 宣告。 這可讓您指定 XML 片段的編碼方式,而不需在 物件上 XmlParserContext 設定它。
Attribute 屬性的值 (引號內的元件) 。
Document 完整的 XML 文件內容。 這會強制執行檔層級規則。

讀取器會使用下列命令來判斷資料流程的編碼方式。

  1. XmlParserContext.Encoding檢查 屬性以判斷編碼方式。

  2. Encoding如果 屬性為 null ,讀取器會檢查資料流程開頭的位元組順序標記。

  3. Encoding如果屬性是 null ,而且找不到位元組順序標記,讀取器會假設資料流程是以 UTF-8 編碼。

另請參閱

適用於

XmlTextReader(String, TextReader)

Source:
XmlTextReader.cs
Source:
XmlTextReader.cs
Source:
XmlTextReader.cs

使用指定的 URL 和 TextReader,初始化 XmlTextReader 類別的新執行個體。

public:
 XmlTextReader(System::String ^ url, System::IO::TextReader ^ input);
public XmlTextReader (string url, System.IO.TextReader input);
new System.Xml.XmlTextReader : string * System.IO.TextReader -> System.Xml.XmlTextReader
Public Sub New (url As String, input As TextReader)

參數

url
String

用來解析外部資源的 URL。 BaseURI 設為這個值。

input
TextReader

TextReader,包含要讀取的 XML 資料。

備註

注意

從 .NET Framework 2.0 開始,建議您使用 XmlReader.Create 方法來建立 XmlReader 實例,以利用新功能。

它假設已將 TextReader 設定為正確的編碼方式。 這是由已在多部分 MIME 案例中從資料流程讀取某些專案的用戶端所使用。

另請參閱

適用於

XmlTextReader(String, Stream)

Source:
XmlTextReader.cs
Source:
XmlTextReader.cs
Source:
XmlTextReader.cs

使用指定的 URL 和資料流,初始化 XmlTextReader 類別的新執行個體。

public:
 XmlTextReader(System::String ^ url, System::IO::Stream ^ input);
public XmlTextReader (string url, System.IO.Stream input);
new System.Xml.XmlTextReader : string * System.IO.Stream -> System.Xml.XmlTextReader
Public Sub New (url As String, input As Stream)

參數

url
String

用來解析外部資源的 URL。 BaseURI 設為這個值。

input
Stream

包含要讀取之 XML 資料的資料流。

例外狀況

inputnull

備註

注意

從 .NET Framework 2.0 開始,建議您使用 XmlReader.Create 方法來建立 XmlReader 實例,以利用新功能。

另請參閱

適用於

XmlTextReader(String, XmlNameTable)

Source:
XmlTextReader.cs
Source:
XmlTextReader.cs
Source:
XmlTextReader.cs

使用指定的檔案和 XmlNameTable,初始化 XmlTextReader 類別的新執行個體。

public:
 XmlTextReader(System::String ^ url, System::Xml::XmlNameTable ^ nt);
public XmlTextReader (string url, System.Xml.XmlNameTable nt);
new System.Xml.XmlTextReader : string * System.Xml.XmlNameTable -> System.Xml.XmlTextReader
Public Sub New (url As String, nt As XmlNameTable)

參數

url
String

包含要讀取之 XML 資料的檔案的 URL。

nt
XmlNameTable

要使用的 XmlNameTable

例外狀況

nt 值為 null

找不到指定的檔案。

找不到部分檔名或目錄。

url 為空字串。

無法解析遠端檔名。

-或-

處理這個要求時發生錯誤。

url 不是有效的 URI。

備註

注意

從 .NET Framework 2.0 開始,建議您使用 XmlReader.Create 方法來建立 XmlReader 實例,以利用新功能。

另請參閱

適用於

XmlTextReader(Stream, XmlNameTable)

Source:
XmlTextReader.cs
Source:
XmlTextReader.cs
Source:
XmlTextReader.cs

使用指定的資料流和 XmlNameTable,初始化 XmlTextReader 類別的新執行個體。

public:
 XmlTextReader(System::IO::Stream ^ input, System::Xml::XmlNameTable ^ nt);
public XmlTextReader (System.IO.Stream input, System.Xml.XmlNameTable nt);
new System.Xml.XmlTextReader : System.IO.Stream * System.Xml.XmlNameTable -> System.Xml.XmlTextReader
Public Sub New (input As Stream, nt As XmlNameTable)

參數

input
Stream

包含要讀取之 XML 資料的資料流。

nt
XmlNameTable

要使用的 XmlNameTable

例外狀況

inputnt 的值為 null

備註

注意

從 .NET Framework 2.0 開始,建議您使用 XmlReader.Create 方法來建立 XmlReader 實例,以利用新功能。

XmlTextReader 使用 System.Text.Encoding 解碼資料流程。

如果您指定名稱資料表,此建構函式會使用該資料表中已定義的名稱。

另請參閱

適用於

XmlTextReader(XmlNameTable)

Source:
XmlTextReader.cs
Source:
XmlTextReader.cs
Source:
XmlTextReader.cs

使用指定的 XmlTextReader 初始化 XmlNameTable 類別的新執行個體。

protected:
 XmlTextReader(System::Xml::XmlNameTable ^ nt);
protected XmlTextReader (System.Xml.XmlNameTable nt);
new System.Xml.XmlTextReader : System.Xml.XmlNameTable -> System.Xml.XmlTextReader
Protected Sub New (nt As XmlNameTable)

參數

nt
XmlNameTable

要使用的 XmlNameTable

備註

注意

從 .NET Framework 2.0 開始,建議您使用 XmlReader.Create 方法來建立 XmlReader 實例,以利用新功能。

另請參閱

適用於

XmlTextReader(String)

Source:
XmlTextReader.cs
Source:
XmlTextReader.cs
Source:
XmlTextReader.cs

使用指定的檔案,初始化 XmlTextReader 類別的新執行個體。

public:
 XmlTextReader(System::String ^ url);
public XmlTextReader (string url);
new System.Xml.XmlTextReader : string -> System.Xml.XmlTextReader
Public Sub New (url As String)

參數

url
String

包含 XML 資料之檔案的 URL。 BaseURI 設為這個值。

例外狀況

找不到指定的檔案。

找不到部分檔名或目錄。

url 為空字串。

無法解析遠端檔名。

-或-

處理這個要求時發生錯誤。

url 不是有效的 URI。

範例

下列範例會讀取 XML 檔案,並顯示每個節點。

#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
   XmlTextReader^ reader = nullptr;
   String^ filename = "items.xml";
   try
   {
      
      // Load the reader with the data file and ignore all white space nodes.         
      reader = gcnew XmlTextReader( filename );
      reader->WhitespaceHandling = WhitespaceHandling::None;
      
      // 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( "<![CDATA[{0}]]>", 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::DocumentType:
               Console::Write( "<!DOCTYPE {0} [{1}]", reader->Name, reader->Value );
               break;

            case XmlNodeType::EntityReference:
               Console::Write( reader->Name );
               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() {

     XmlTextReader reader = null;

     try {

        // Load the reader with the data file and ignore all white space nodes.
        reader = new XmlTextReader(filename);
        reader.WhitespaceHandling = WhitespaceHandling.None;

        // 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("<![CDATA[{0}]]>", 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.DocumentType:
               Console.Write("<!DOCTYPE {0} [{1}]", reader.Name, reader.Value);
               break;
             case XmlNodeType.EntityReference:
               Console.Write(reader.Name);
               break;
             case XmlNodeType.EndElement:
               Console.Write("</{0}>", reader.Name);
               break;
           }
        }
     }

     finally {
        if (reader!=null)
          reader.Close();
     }
  }
} // End class
Option Strict
Option Explicit

Imports System.IO
Imports System.Xml

'Reads an XML document
Public Class Sample
    Private Const filename As String = "items.xml"
    
    Public Shared Sub Main()
        Dim reader As XmlTextReader = Nothing
        
        Try
            ' Load the reader with the data file and ignore all white space nodes.         
            reader = New XmlTextReader(filename)
            reader.WhitespaceHandling = WhitespaceHandling.None
            
            ' 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("<![CDATA[{0}]]>", 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.DocumentType
                        Console.Write("<!DOCTYPE {0} [{1}]", reader.Name, reader.Value)
                    Case XmlNodeType.EntityReference
                        Console.Write(reader.Name)
                    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 an char entity: A</Item>
  <!-- Fourteen chars in this element.-->
  <Item>1234567890ABCD</Item>
</Items>

備註

注意

從 .NET Framework 2.0 開始,建議您使用 XmlReader.Create 方法來建立 XmlReader 實例,以利用新功能。

如果檔案位於需要存取認證的資源上,請使用 XmlResolver 屬性來指定必要的認證。

注意

在 .NET Framework 1.1 版中,部分信任的程式碼無法設定 XmlResolver 屬性。 因應措施是使用必要的認證建立 XmlUrlResolver 、將 URI 傳遞至 XmlUrlResolver.GetEntity 方法,然後使用產生的 Stream 物件來建構 XmlTextReader 。 下列 C# 程式碼會說明因應措施。

// Create a resolver with the necessary credentials.
XmlUrlResolver resolver = new XmlUrlResolver();
NetworkCredential nc = new NetworkCredential(SecurelyStoredUserName, SecurelyStoredPassword, SecurelyStoredDomain);
resolver.Credentials = nc;
// Get a Stream object containing the XML file.
Uri myUri = new Uri ("http://myServer/data/books.xml");
Stream s=(Stream)resolver.GetEntity(myUri, null, typeof(Stream));
// Construct a reader using the Stream object.
XmlTextReader reader = new XmlTextReader(s);

另請參閱

適用於

XmlTextReader(TextReader)

Source:
XmlTextReader.cs
Source:
XmlTextReader.cs
Source:
XmlTextReader.cs

使用指定的 XmlTextReader 初始化 TextReader 類別的新執行個體。

public:
 XmlTextReader(System::IO::TextReader ^ input);
public XmlTextReader (System.IO.TextReader input);
new System.Xml.XmlTextReader : System.IO.TextReader -> System.Xml.XmlTextReader
Public Sub New (input As TextReader)

參數

input
TextReader

TextReader,包含要讀取的 XML 資料。

範例

下列範例會使用 StringReader 類別, XmlTextReader 將 XML 字串載入 物件。

#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
   String^ xmlData = "<book>\r\n       <title>Oberon's Legacy</title>\r\n       <price>5.95</price>\r\n      </book>";
   
   // Create the reader.
   XmlTextReader^ reader = gcnew XmlTextReader( gcnew StringReader( xmlData ) );
   reader->WhitespaceHandling = WhitespaceHandling::None;
   
   // Display each element node.
   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::EndElement:
            Console::Write( "</{0}>", reader->Name );
            break;
      }
   }

   
   // Close the reader.
   reader->Close();
}
using System;
using System.IO;
using System.Xml;

public class Sample {

  public static void Main() {

    string xmlData =
    @"<book>
       <title>Oberon's Legacy</title>
       <price>5.95</price>
      </book>";

    // Create the reader.
    XmlTextReader reader = new XmlTextReader(new StringReader(xmlData));
    reader.WhitespaceHandling = WhitespaceHandling.None;

    // Display each element node.
    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.EndElement:
           Console.Write("</{0}>", reader.Name);
           break;
      }
    }

    // Close the reader.
    reader.Close();
  }
} // End class
Imports System.IO
Imports System.Xml

public class Sample 

  public shared sub Main()

    Dim xmlData as string 
    xmlData = "<book>" & _
              "  <title>Oberon's Legacy</title>" & _
              "  <price>5.95</price>" & _
              "</book>"

    ' Create the reader.
    Dim reader as XmlTextReader = new XmlTextReader(new StringReader(xmlData))
    reader.WhitespaceHandling = WhitespaceHandling.None

    ' Display each element node.
    while reader.Read()
       select case reader.NodeType
         case XmlNodeType.Element
           Console.Write("<{0}>", reader.Name)
         case XmlNodeType.Text
           Console.Write(reader.Value)
         case XmlNodeType.EndElement
           Console.Write("</{0}>", reader.Name)
       end select       
    end while           

    ' Close the reader.
    reader.Close()       
  end sub
end class

備註

注意

從 .NET Framework 2.0 開始,建議您使用 XmlReader.Create 方法來建立 XmlReader 實例,以利用新功能。

它假設已將 TextReader 設定為正確的編碼方式。 這是由已在多部分 MIME 案例中從資料流程讀取某些專案的用戶端所使用。

另請參閱

適用於

XmlTextReader(Stream)

Source:
XmlTextReader.cs
Source:
XmlTextReader.cs
Source:
XmlTextReader.cs

使用指定的資料流初始化 XmlTextReader 類別的新執行個體。

public:
 XmlTextReader(System::IO::Stream ^ input);
public XmlTextReader (System.IO.Stream input);
new System.Xml.XmlTextReader : System.IO.Stream -> System.Xml.XmlTextReader
Public Sub New (input As Stream)

參數

input
Stream

包含要讀取之 XML 資料的資料流。

例外狀況

inputnull

備註

注意

從 .NET Framework 2.0 開始,建議您使用 XmlReader.Create 方法來建立 XmlReader 實例,以利用新功能。

XmlTextReader 使用 System.Text.Encoding 解碼資料流程。

另請參閱

適用於

XmlTextReader(TextReader, XmlNameTable)

Source:
XmlTextReader.cs
Source:
XmlTextReader.cs
Source:
XmlTextReader.cs

使用指定的 XmlTextReaderTextReader,初始化 XmlNameTable 類別的新執行個體。

public:
 XmlTextReader(System::IO::TextReader ^ input, System::Xml::XmlNameTable ^ nt);
public XmlTextReader (System.IO.TextReader input, System.Xml.XmlNameTable nt);
new System.Xml.XmlTextReader : System.IO.TextReader * System.Xml.XmlNameTable -> System.Xml.XmlTextReader
Public Sub New (input As TextReader, nt As XmlNameTable)

參數

input
TextReader

TextReader,包含要讀取的 XML 資料。

nt
XmlNameTable

要使用的 XmlNameTable

例外狀況

nt 值為 null

備註

注意

從 .NET Framework 2.0 開始,建議您使用 XmlReader.Create 方法來建立 XmlReader 實例,以利用新功能。

它假設已將 TextReader 設定為正確的編碼方式。 這是由已在多部分 MIME 案例中從資料流程讀取某些專案的用戶端所使用。

另請參閱

適用於