How to find an XML node from a line and column number in C#?

后端 未结 2 365
深忆病人
深忆病人 2021-01-14 00:59

Given the following

  • A line number
  • A column number
  • An XML file

(Where the line and column number represent the \'<\' charact

2条回答
  •  孤城傲影
    2021-01-14 01:22

    See LINQ to XML and Line Numbers on LINQ Exchange gives an example using IXmlLineInfo that corresponds to what you're looking for:

    XDocument xml = XDocument.Load(fileName, LoadOptions.SetLineInfo);
    var line = from x in xml.Descendants()
               let lineInfo = (IXmlLineInfo)x
               where lineInfo.LineNumber == 21
               select x;
    
    foreach (var item in line)
    {
        Console.WriteLine(item);
    }
    

提交回复
热议问题