开发者

How to get the character location of a XmlElement?

Let开发者_如何学Go'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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜