find line number of specific tag of xml file [duplicate]

痴心易碎 提交于 2019-12-13 09:35:11

问题


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.


回答1:


You can do it with 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);


来源:https://stackoverflow.com/questions/35504337/find-line-number-of-specific-tag-of-xml-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!