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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/621059/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 10:36:41  来源:igfitidea点击:

Current line number from a System.Xml.XmlReader (C# & .Net)

c#.netxmlxmlreaderline-numbers

提问by Danielb

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.

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

采纳答案by Daniel LeCheminant

Take advantage of the IXmlLineInfointerface supported by an XmlReader:

利用IXmlLineInfo支持的接口XmlReader

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

回答by GHC

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

扩展IXmlLineInfo界面,这方​​面的文档很糟糕;通过一些挖掘,我可以告诉你以下内容:

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

1)System.Xml.XmlReader是抽象的,所以你永远不会处理这样的一个实例,因此,它没有实现的事实并不是IXmlLineInfo很重要(尽管,如果它实现了,它会使一切变得那么小容易一点:) )

2) The System.Xml.IXmlLineInfointerface provides two properties: LineNumberand 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.

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

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

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.

Looking at the above list, the XmlDictionaryReaderis going to be used internally, the XmlNodeReaderis 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 XmlTextReaderand 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.

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

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):

话虽如此,文档似乎很轻。刚刚完成此操作后,我最终完成了(_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() + ")";
    }

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.XmlValidatingReaderor System.Xml.XmlTextReader. As such, it's probably wise to use an approach like the above.

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