开发者

get line number for XElement here

XDocument xdoc = XDocument.Load(f开发者_如何学Goile);
IEnumerable<XElement> categories = xdoc.Descendants("Category");
foreach (XElement category in categories)
{
    //get line number for element here...
}


XElement (actually XObject) implements the IXmlLineInfo interface, so you can use it:

IXmlLineInfo info = category;
int lineNumber = info.LineNumber;

Note that line information is not always available, you need to call the HasLineInfo method to check if the information is available. You can specify LoadOptions.SetLineInfo when you load the document with XDocument.Load


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜