Get Line Number for Xelement Here

get line number for XElement here

XDocument xdoc = XDocument.Load(file, LoadOptions.SetLineInfo);
IEnumerable<XElement> categories = xdoc.Descendants("Category");
foreach (XElement category in categories)
{
//get line number for element here...
string lineNumber = ((IXmlLineInfo)category).HasLineInfo() ? ((IXmlLineInfo)category).LineNumber : -1;
}

How to get XElement from XDocument by Line Number

You can read your file to string array

string[] lines = File.ReadAllLines("path/to/file");

And then get your line like lines[4].

Or you should better look at XPath as your XML document can change.

Take a look at these exaples and tutorials: XPath Examples, Selecting Nodes.

How to get line number from an XmlElement or a XPathNavigator

It works fine for me with XPathNavigator over an XPathDocument, the snippet

    XPathDocument doc = new XPathDocument("XMLFile1.xml");
foreach (XPathNavigator element in doc.CreateNavigator().Select("//*"))
{
Console.WriteLine("Element {0} at line {1}.", element.Name, (IXmlLineInfo)element != null ? ((IXmlLineInfo)element).LineNumber : 0);
}

for the file

<?xml version="1.0" encoding="utf-8" ?>
<root>
<foo>3</foo>
<foo>4</foo>
</root>

outputs

Element root at line 2.
Element foo at line 3.
Element foo at line 4.

I think for XmlDocument you would need to extend the DOM implementation. If you want to manipulate an XML document then these days in the .NET world I would use XDocument/XElement, there you can set load options to ensure you get an IXmlLineInfo, see http://msdn.microsoft.com/en-us/library/bb538371%28v=vs.110%29.aspx and http://msdn.microsoft.com/en-us/library/system.xml.linq.loadoptions%28v=vs.110%29.aspx.

XDocument losing line number

The LineInformations are not always loaded, when you load xml via XDocument.

You need to specify that you also want to load the LineInformation when you load the XML. That is done by using one of the Load methods that you can pass in a value of LoadOptions of the XDocument class.

var document = XDocument.Load(file, LoadOptions.SetLineInfo);

How to get the Line number where the xml tag is closed?

I found a solution to get the the line number where employee xml tag is closing.

foreach (var employee in employees)
{
// code to get employee tag start Line number
var elemntStartLine = ((IXmlLineInfo)employee).LineNumber;
// here i get tag end Line number from xml file.
var elemntEndLine =GetXelementEndLineNo(employee);
}

 

 public int GetXelementEndLineNo(XElement xElement)
{
int endLineNo = 0;
xElement.RemoveAll();
xElement.Add(string.Empty);
using (XmlReader xmlReader = xElement.CreateReader())
{
var lineInfo = ((IXmlLineInfo)xmlReader);
while (xmlReader.Read())
{
if (xmlReader.NodeType == XmlNodeType.EndElement && xmlReader.LocalName.ToUpper() == "EMPLOYEE")
{
endLineNo = lineInfo.LineNumber;
}
}
}
return endLineNo;
}

How to Get Element Names from XElement Using C#?

XDocument xd = XDocument.Load("..\\Cmd.xml");
IEnumerable<string> names = xd.XPathSelectElements("//Data/*")
.Select(e => e.Name.LocalName);

Or without XPath

IEnumerable<string> names = xd.Descendants("Data")      
.Elements()
.Select(e => e.Name.LocalName);

Result:

Test
Test1
Test3
Test4


Related Topics



Leave a reply



Submit