C# 如何查找末尾带有空格的节点并获取行位置?

C# 如何查找末尾带有空格的节点并获取行位置?,c#,linq-to-xml,C#,Linq To Xml,如何使用(IXmlLineInfo)的LinePosition属性在xml文件中搜索任何标记的close标记之前的空格并获取其实际位置 例如,对于下面的xml文件 <?xml version="1.0" encoding="utf-8"?> <main> <child1>anything</child1> <child2>whatever </child1> <pp> &l

如何使用
(IXmlLineInfo)
LinePosition
属性在xml文件中搜索任何标记的close标记之前的空格并获取其实际位置 例如,对于下面的xml文件

<?xml version="1.0" encoding="utf-8"?>
<main>
    <child1>anything</child1>
    <child2>whatever </child1>
    <pp>
        <ccl>i dont know</ccl>
        <ccp>1253 </ccp>
    </pp>
</main>

使用
XDocument
最简单的方法是什么?

第一个任务是找到以空格结尾的所有节点,然后通过将它们强制转换到
IXmlLineInfo
来获取行信息,假设文档是
XDocument

document.Descendants()
        .Where(node => ((string)node).EndsWith(" "))
        .Select(node => node as IXmlLineInfo);

这将为您提供包含节点行号和位置的行信息。如果要查找空间的行位置,则需要对该值进行一些计算,即将节点值的长度添加到行位置,即可获得空白的位置。此外,您可能需要添加节点名称的长度,包括开始和结束标记,以找到准确的位置。

如果出现类似
abc30
的情况,您的代码同时显示
age
profile
节点,但在这种情况下,我只想要
age
。我如何才能做到这一点……我也在努力获取准确的行位置很好,你能帮忙吗??
document.Descendants()
        .Where(node => ((string)node).EndsWith(" "))
        .Select(node => node as IXmlLineInfo);