小编典典

在此获取XElement的行号

c#

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

阅读 548

收藏
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