System.Xml.XmlReader 中的当前行号(C# 和 .Net)
有谁知道如何获取 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
利用
IXmlLineInfo
XmlReader 支持的接口:Take advantage of the
IXmlLineInfo
interface supported by anXmlReader
:扩展 IXmlLineInfo 接口,相关文档非常糟糕; 经过一番挖掘,我可以告诉您以下内容:
1)
System.Xml.XmlReader
是抽象的,因此您永远不会处理此实例,因此,事实上,它没有实现IXmlLineInfo
并不是很重要(尽管,如果它实现了,它会让一切变得更容易一点:))2)
System.Xml.IXmlLineInfo
接口提供了两个属性:LineNumber
和LinePosition
(这是我们关心的),以及一个方法:HasLineInfo()
根据文档,它会让您知道实现者是否可以返回线路信息。3) 在
System.Xml.XmlReader
的已记录继承者中,我们有:查看上面的列表,
XmlDictionaryReader
将在内部使用,XmlNodeReader当您传入要读取的节点(该节点已被解析,已脱离其源文档)时,将使用
、XmlTextReader
和XmlValidtingReader
(两者都实现了IXmlLineInfo
),将在您读取文档时使用。 因此,总而言之,如果向您提供位置信息是可能的或有用的,框架就会这样做。话虽这么说,文档似乎很轻。 刚刚完成此操作后,我最终做了(_xr 是 System.Xml.XmlReader 的未知具体实现):
话虽如此,当我实际运行上面的代码时, _xr 最终成为
System.Xml.XsdValidatingReader
(祝你好运找到相关文档!),它继承自System.Xml.XmlReader
,但不继承自System.Xml.XmlValidatingReader
或System.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 implementIXmlLineInfo
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
andLinePosition
(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:Looking at the above list, the
XmlDictionaryReader
is going to be used internally, theXmlNodeReader
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), theXmlTextReader
andXmlValidtingReader
(both of which implementIXmlLineInfo
), 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
):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 fromSystem.Xml.XmlReader
, but doesn't inherit fromSystem.Xml.XmlValidatingReader
orSystem.Xml.XmlTextReader
. As such, it's probably wise to use an approach like the above.使用 IXmlLineInfo 和“现代”c#:
A small refinement on Daniel's long standing answer, using IXmlLineInfo and 'modern' c#: