查找xml文件特定标签的行号 [英] find line number of specific tag of xml file
本文介绍了查找xml文件特定标签的行号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何查找xml文件特定标签的标签和行号,例如:
How to find tag and line number of Specific tag of xml file, for example:
<sec id="fm.s2">
<title>Acknowledgments</title>
</sec>
<p>Center for Evidence and Practice Improvement</p>
<p>Agency for Healthcare Research and Quality</p>
<sec id="fm.s2">
<title>Director</title>
</sec>
<p>Center for Evidence and Practice Improvement</p>
<p>Agency for Healthcare Research and Quality</p>
<sec id="fm.s2">
<title>Acknowledgments</title>
</sec>
在此示例中,我想获取
包含致谢"文本的行号.
In this example, I want to get line no of which <title>
contains "Acknowledgments" text.
推荐答案
你可以用 linq
var xml = XDocument.Load(@"path", LoadOptions.SetLineInfo);
var lineNumbers = xml.Descendants()
.Where(x =>!x.Descendants().Any() && //exact node contains the value
x.Value.Contains("Acknowledgments"))
.Cast<IXmlLineInfo>()
.Select(x => x.LineNumber);
这篇关于查找xml文件特定标签的行号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文