XmlTextReader.LineNumber 属性

定义

获取当前行号。

public:
 property int LineNumber { int get(); };
public int LineNumber { get; }
member this.LineNumber : int
Public ReadOnly Property LineNumber As Integer

属性值

当前行号。

实现

示例

以下示例显示每个节点,包括其深度、行号和行位置。

#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>\n"
   "<misc>\n"
   "<style>paperback</style>\n"
   "<pages>240</pages>\n"
   "</misc>\n"
   "</book>\n";
   
   // Create the XmlNamespaceManager.
   NameTable^ nt = gcnew NameTable;
   XmlNamespaceManager^ nsmgr = gcnew XmlNamespaceManager( nt );
   
   // 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 and display each node.
   while ( reader->Read() )
   {
      switch ( reader->NodeType )
      {
         case XmlNodeType::Element:
            Console::Write( " {0} {1}, {2}  ", reader->Depth, reader->LineNumber, reader->LinePosition );
            Console::WriteLine( "< {0}>", reader->Name );
            break;

         case XmlNodeType::Text:
            Console::Write( " {0} {1}, {2}  ", reader->Depth, reader->LineNumber, reader->LinePosition );
            Console::WriteLine( " {0}", reader->Value );
            break;

         case XmlNodeType::EndElement:
            Console::Write( " {0} {1}, {2}  ", reader->Depth, reader->LineNumber, reader->LinePosition );
            Console::WriteLine( "</ {0}>", reader->Name );
            break;
      }
   }

   
   // 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>
         <misc>
           <style>paperback</style>
           <pages>240</pages>
         </misc>
        </book>";

    // Create the XmlNamespaceManager.
    NameTable nt = new NameTable();
    XmlNamespaceManager nsmgr = new XmlNamespaceManager(nt);

    // 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 and display each node.
    while (reader.Read()){
       switch (reader.NodeType){
         case XmlNodeType.Element:
           Console.Write("{0} {1},{2}  ", reader.Depth, reader.LineNumber, reader.LinePosition);
           Console.WriteLine("<{0}>", reader.Name);
           break;
         case XmlNodeType.Text:
           Console.Write("{0} {1},{2}  ", reader.Depth, reader.LineNumber, reader.LinePosition);
           Console.WriteLine("  {0}", reader.Value);
           break;
         case XmlNodeType.EndElement:
           Console.Write("{0} {1},{2}  ", reader.Depth, reader.LineNumber, reader.LinePosition);
           Console.WriteLine("</{0}>", reader.Name);
           break;
       }
    }

    // Close the reader.
    reader.Close();
  }
}
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>" + Chr(10) & _
                                    "  <misc>"  + Chr(10) & _
                                    "    <style>paperback</style>"  + Chr(10) & _
                                    "    <pages>240</pages>" + Chr(10) & _
                                    "  </misc>" + Chr(10) & _
                                    "</book>"

    ' Create the XmlNamespaceManager.
    Dim nt as NameTable = new NameTable()
    Dim nsmgr as XmlNamespaceManager = new XmlNamespaceManager(nt)

    ' 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 and display each node.
    while (reader.Read())
       select case reader.NodeType
         case XmlNodeType.Element:
           Console.Write("{0} {1},{2}  ", reader.Depth, reader.LineNumber, reader.LinePosition)
           Console.WriteLine("<{0}>", reader.Name)
         case XmlNodeType.Text:
           Console.Write("{0} {1},{2}  ", reader.Depth, reader.LineNumber, reader.LinePosition)
           Console.WriteLine("  {0}", reader.Value)
         case XmlNodeType.EndElement:
           Console.Write("{0} {1},{2}  ", reader.Depth, reader.LineNumber, reader.LinePosition)
           Console.WriteLine("</{0}>", reader.Name)
       end select       
    end while           

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

注解

注意

从 .NET Framework 2.0 开始,建议使用 XmlReader.Create 方法创建XmlReader实例,以利用新功能。

此属性最常用于错误报告,但可以随时调用。 此属性 1的起始值为 。

LinePosition结合使用时,值 指示 1,1 文档的开头。

适用于

另请参阅