gpt4 book ai didi

c# - 在此处获取 XElement 的行号

转载 作者:IT王子 更新时间:2023-10-29 04:15:09 25 4
gpt4 key购买 nike

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

最佳答案

XElement(实际上是 XObject)实现了 IXmlLineInfo 接口(interface),因此您可以使用它:

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

请注意,线路信息并不总是可用的,您需要调用HasLineInfo 方法来检查信息是否可用。您可以在使用 XDocument.Load

加载文档时指定 LoadOptions.SetLineInfo

关于c# - 在此处获取 XElement 的行号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4471001/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com