As Jon Skeet says, XmlTextReader
implements IXmlLineInfo
but XmlTextReader
was deprecated since .NET 2.0
and the question is about XmlReader
only.
I found this solution:
XmlReader xr = XmlReader.Create( // MSDN recommends to use Create() instead of ctor()
new StringReader("<some><xml><string><data>"),
someSettings // furthermore, can't set XmlSettings on XmlTextReader
);
IXmlLineInfo xli = (IXmlLineInfo)xr;
while (xr.Read())
{
// ... some read actions ...
// current position in StringReader can be accessed through
int line = xli.LineNumber;
int pos = xli.LinePosition;
}
P.S. Tested for .NET Compact Framework 3.5, but should work for others too.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…