Get line number and line position in CSharp

Description

The following code shows how to get line number and line position.

Example


//w ww.j  a  v  a 2 s.  c o  m
using System;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
using System.Collections;
using System.Collections.Generic;


public class MainClass
{
    public static void Main()
    {
        XElement po = XElement.Load("PurchaseOrder.xml", LoadOptions.SetBaseUri | LoadOptions.SetLineInfo);

        foreach (XElement e in po.DescendantsAndSelf())
        {
            Console.WriteLine(e.Ancestors().Count());
            Console.WriteLine(e.Name);
            Console.WriteLine(((IXmlLineInfo)e).LineNumber);
            Console.WriteLine(((IXmlLineInfo)e).LinePosition);
        }
    }
}




















Home »
  C# Tutorial »
    XML »




Load Parse
Document
Element
Attribute
Namespace
Query
Save
Schema
Style