System.Xml.IXmlLineInfo Interface

Assembly: System.Xml.dll
Namespace: System.Xml
Summary
Provides an interface to enable a class to return line and position information.
C# Syntax:
public interface IXmlLineInfo
Example
The following example parses an XML fragment. Each node is displayed including its depth, line number, and line position.
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.
    XmlNamespaceManager nsmgr = new XmlNamespaceManager(new NameTable());

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

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

    IXmlLineInfo lineInfo = ((IXmlLineInfo)reader);
    if (lineInfo.HasLineInfo()){
       
      // Parse the XML and display each node.
      while (reader.Read()){
       switch (reader.NodeType){
         case XmlNodeType.Element:
           Console.Write("{0} {1},{2}  ", reader.Depth, lineInfo.LineNumber, lineInfo.LinePosition);
           Console.WriteLine("<{0}>", reader.Name);
           break;
         case XmlNodeType.Text:
           Console.Write("{0} {1},{2}  ", reader.Depth, lineInfo.LineNumber, lineInfo.LinePosition);
           Console.WriteLine("  {0}", reader.Value);
           break;
         case XmlNodeType.EndElement:
           Console.Write("{0} {1},{2}  ", reader.Depth, lineInfo.LineNumber, lineInfo.LinePosition);
           Console.WriteLine("</{0}>", reader.Name);
           break;
       }       
     }           
    }

    // Close the reader.
    reader.Close();       
  }
}

    
See also:
System.Xml Namespace

System.Xml.IXmlLineInfo Member List:

Public Properties
LineNumber Read-only

Gets the current line number.
LinePosition Read-only

Gets the current line position.
Public Methods
HasLineInfo

System.Xml.IXmlLineInfo Member Details

Property: LineNumber (read-only)
Summary
Gets the current line number.
C# Syntax:
int LineNumber {get;}
Remarks
This property is used primarily for error reporting, but can be called at any time. The starting value is 1. Combined with IXmlLineInfo.LinePosition, a value of 1,1 indicates the start of a document.

Return to top


Property: LinePosition (read-only)
Summary
Gets the current line position.
C# Syntax:
int LinePosition {get;}
Remarks
This property is used primarily for error reporting, but can be called at any time. The starting value is 1. Combined with IXmlLineInfo.LineNumber, a value of 1,1 indicates the start of a document.

Return to top


Method: HasLineInfo()
Summary
Gets a value indicating whether the class can return line information.
C# Syntax:
bool HasLineInfo();
Return Value:
true if IXmlLineInfo.LineNumber and IXmlLineInfo.LinePosition can be provided; otherwise, false.

Return to top


Top of page

Copyright (c) 2002 Microsoft Corporation. All rights reserved.