Sil*_*ude 5 c# xml location character
假设在我的 C# 代码中,我从XmlDocument(或XDocument)中检索了一个XmlElement(或XElement)。如何在 XML 文件中获取此 XmlElement的字符位置?
换句话说,我想被告知
"Your element starts on the 176th character in the text file containing the XML",
Run Code Online (Sandbox Code Playgroud)
不是
"Your 'book' element is the 3rd 'book' element in the whole XML document".
Run Code Online (Sandbox Code Playgroud)
我不确定这是否可以确定字符号,但您可以在行内找到行号和位置:
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);
Run Code Online (Sandbox Code Playgroud)