如何查找xml文件的特定标签的标签和行号, 例如:
<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>
在这个例子中,我想获得<title>
包含“Acknowledgements”文本的行号。
答案 0 :(得分:1)
你可以用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);