一尘不染

在此获取XElement的行号

c#

XDocument xdoc = XDocument.Load(file);
IEnumerable<XElement> categories = xdoc.Descendants("Category");
foreach (XElement category in categories)
{
    //get line number for element here...
}

阅读 480

收藏
2020-05-19

共1个答案

一尘不染

XDocument xdoc = XDocument.Load(file, LoadOptions.SetLineInfo);
IEnumerable<XElement> categories = xdoc.Descendants("Category");
foreach (XElement category in categories)
{
    //get line number for element here...
    string lineNumber = ((IXmlLineInfo)category).HasLineInfo() ? ((IXmlLineInfo)category).LineNumber : -1;
}
2020-05-19