System.Xml.XmlReader 中的当前行号(C# 和 .Net)

发布于 2024-07-15 11:02:50 字数 67 浏览 11 评论 0原文

有谁知道如何获取 System.Xml.XmlReader 的当前行号? 我试图记录在文件中找到 Xml 元素的位置。

Does anyone know how I can get the current line number of an System.Xml.XmlReader? I am trying to record where in a file I find Xml elements.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

意中人 2024-07-22 11:02:50

利用 IXmlLineInfo XmlReader 支持的接口:

IXmlLineInfo xmlInfo = (IXmlLineInfo)reader;
int lineNumber = xmlInfo.LineNumber;

Take advantage of the IXmlLineInfo interface supported by an XmlReader:

IXmlLineInfo xmlInfo = (IXmlLineInfo)reader;
int lineNumber = xmlInfo.LineNumber;
七色彩虹 2024-07-22 11:02:50

扩展 IXmlLineInfo 接口,相关文档非常糟糕; 经过一番挖掘,我可以告诉您以下内容:

1) System.Xml.XmlReader 是抽象的,因此您永远不会处理此实例,因此,事实上,它没有实现 IXmlLineInfo 并不是很重要(尽管,如果它实现了,它会让一切变得更容易一点:))

2) System.Xml.IXmlLineInfo 接口提供了两个属性:LineNumberLinePosition(这是我们关心的),以及一个方法:HasLineInfo()根据文档,它会让您知道实现者是否可以返回线路信息。

3) 在 System.Xml.XmlReader 的已记录继承者中,我们有:

System.Xml.XmlDictionaryReader - abstract, used by WCF for serialization, no IXmlLineInfo
System.Xml.XmlNodeReader - used when reading a node, no IXmlLineInfo
System.Xml.XmlTextReader - used when reading a stream of data, has IXmlLineInfo
System.Xml.XmlValidatingReader - used when reading a stream of data and validating, has IXmlLineInfo.

查看上面的列表,XmlDictionaryReader 将在内部使用,XmlNodeReader当您传入要读取的节点(该节点已被解析,已脱离其源文档)时,将使用XmlTextReaderXmlValidtingReader(两者都实现了IXmlLineInfo),将在您读取文档时使用。 因此,总而言之,如果向您提供位置信息是可能的或有用的,框架就会这样做。

话虽这么说,文档似乎很轻。 刚刚完成此操作后,我最终做了(_xr 是 System.Xml.XmlReader 的未知具体实现):

string position = "(unknown)";
    if (_xr != null && typeof(System.Xml.IXmlLineInfo).IsInstanceOfType(_xr) &&
((System.Xml.IXmlLineInfo)_xr).HasLineInfo())
    {
        System.Xml.IXmlLineInfo li = (System.Xml.IXmlLineInfo)_xr;
        position = "(" + li.LineNumber.ToString() + "," + li.LinePosition.ToString() + ")";
    }

话虽如此,当我实际运行上面的代码时, _xr 最终成为 System.Xml.XsdValidatingReader (祝你好运找到相关文档!),它继承自 System.Xml.XmlReader,但不继承自 System.Xml.XmlValidatingReaderSystem.Xml.XmlTextReader。 因此,使用上述方法可能是明智的。

Expanding on the IXmlLineInfo interface, the documentation for this is pretty bad; from doing a bit of digging, I can tell you the following:

1) System.Xml.XmlReader is abstract, so you're never going to be dealing with an instance of this, as such, the fact that it doesn't implement IXmlLineInfo isn't massively concerning (although, if it did, it would make everything just that little bit easier :) )

2) The System.Xml.IXmlLineInfo interface provides two properties: LineNumber and LinePosition (which are the things we care about), plus a method: HasLineInfo() which, according to the documentation, will let you know if an implementor can return the lineinfo.

3) Of the documented inheritors of System.Xml.XmlReader, we have:

System.Xml.XmlDictionaryReader - abstract, used by WCF for serialization, no IXmlLineInfo
System.Xml.XmlNodeReader - used when reading a node, no IXmlLineInfo
System.Xml.XmlTextReader - used when reading a stream of data, has IXmlLineInfo
System.Xml.XmlValidatingReader - used when reading a stream of data and validating, has IXmlLineInfo.

Looking at the above list, the XmlDictionaryReader is going to be used internally, the XmlNodeReader is going to be used when you've passed in a node to be read (which, having been parsed, is already untethered from its source document), the XmlTextReader and XmlValidtingReader (both of which implement IXmlLineInfo), are going to be used when you're reading from a document. So, the long and the short of it seems to be that if it's possible or useful to give you position information, the framework will do so.

That being said, the documentation seems to be very light. Having just done this, I ended up doing (with _xr being an unknown concrete implementation of System.Xml.XmlReader):

string position = "(unknown)";
    if (_xr != null && typeof(System.Xml.IXmlLineInfo).IsInstanceOfType(_xr) &&
((System.Xml.IXmlLineInfo)_xr).HasLineInfo())
    {
        System.Xml.IXmlLineInfo li = (System.Xml.IXmlLineInfo)_xr;
        position = "(" + li.LineNumber.ToString() + "," + li.LinePosition.ToString() + ")";
    }

With all of that being said, when I actually run the code above, the type of _xr ends up being System.Xml.XsdValidatingReader (good luck finding documentation on that!), which inherits from System.Xml.XmlReader, but doesn't inherit from System.Xml.XmlValidatingReader or System.Xml.XmlTextReader. As such, it's probably wise to use an approach like the above.

乜一 2024-07-22 11:02:50

使用 IXmlLineInfo 和“现代”c#:

int lineNumber = (reader is IXmlLineInfo xmlLine && xmlLine.HasLineInfo()) ? xmlLine.LineNumber : -1;

A small refinement on Daniel's long standing answer, using IXmlLineInfo and 'modern' c#:

int lineNumber = (reader is IXmlLineInfo xmlLine && xmlLine.HasLineInfo()) ? xmlLine.LineNumber : -1;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文