考虑到以下情况
(其中行号和列号表示节点的“<”字符)
使用XDocument API,如何在该位置找到XNode。
发布于 2012-02-10 23:26:09
你可以这样做:
XNode FindNode(string path, int line, int column)
{
XDocument doc = XDocument.Load(path, LoadOptions.SetLineInfo);
var query =
from node in doc.DescendantNodes()
let lineInfo = (IXmlLineInfo)node
where lineInfo.LineNumber == line
&& lineInfo.LinePosition <= column
select node;
return query.LastOrDefault();
}发布于 2012-02-10 23:20:41
参见LINQ上的LINQ到XML和行号给出了一个使用IXmlLineInfo的示例,该示例对应于您要寻找的内容:
XDocument xml = XDocument.Load(fileName, LoadOptions.SetLineInfo);
var line = from x in xml.Descendants()
let lineInfo = (IXmlLineInfo)x
where lineInfo.LineNumber == 21
select x;
foreach (var item in line)
{
Console.WriteLine(item);
}https://stackoverflow.com/questions/9226108
复制相似问题