2012-02-10 64 views

回答

3

你可以做這樣的事情:

XNode FindNode(string path, int line, int column) 
{ 
    XDocument doc = XDocument.Load(path, LoadOptions.SetLineInfo); 
    var query = 
     from node in doc.DescendantNodes() 
     let lineInfo = (IXmlLineInfo)node 
     where lineInfo.LineNumber == line 
     && lineInfo.LinePosition <= column 
     select node; 
    return query.LastOrDefault(); 
} 
1

LINQ to XML and Line Numbers LINQ的交流提供了使用IXmlLineInfo對應於您要尋找的一個例子:

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); 
} 
+0

@KenWhite確定。已更新爲包含從文章複製的相關代碼塊。 – JamieSee 2012-02-10 23:48:04

相關問題