Let's say in my C# code I have retrieved a XmlElement (or XElement) from a XmlDocument (or XDocument). How do I get the character location of this XmlElement in the XML file?
In other words, I want to be told
"Your element starts on the 176th character in the text file containing the XML",
not
"Your 'book' element is the 3rd 'book' element in the whole XML document".
I'm not sure if this is possible to determine the char number, but you can find line number and position inside of the line:
var document = XDocument.Load(fileName, LoadOptions.SetLineInfo);
var element = document.Descendants("nodeName").FirstOrDefault();
var xmlLineInfo = (IXmlLineInfo)element;
Console.WriteLine("Line: {0}, Position: {1}", xmlLineInfo.LineNumber, xmlLineInfo.LinePosition);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With