从 XmlTextReader 检索流文件名
这有点微不足道,但就这样吧。我正在使用 .Net 4.0 框架将 XmlTextReader 对象传递给方法。
public void TestMethod(XmlTextReader reader)
{
try
{
//...
//Logic
//...
}
catch(Exception ex)
{
//I also want to log the file location of the XmlTextReader!
Log(ex.Message);
}
}
如果阅读器发生问题,我想记录 XmlTextReader 正在读取的文件的位置。有没有一种简单的方法可以返回 XmlTextReader 正在使用的流?它有点微不足道的原因是,我可以轻松地将附加字符串传递到包含用于创建流的文件位置的方法,但似乎这必须是仅使用 XmlTextReader 的方法。
谢谢!
更新,这实际上是我正在做的事情......抱歉这个不好的例子:
public void TestMethod(XmlTextReader reader)
{
//...
//Logic
//...
if(something_inside_the_XML)
throw new Exception(FileLocation);
}
This is somewhat trivial but here goes. I am passing an XmlTextReader object to a method using .Net 4.0 framework.
public void TestMethod(XmlTextReader reader)
{
try
{
//...
//Logic
//...
}
catch(Exception ex)
{
//I also want to log the file location of the XmlTextReader!
Log(ex.Message);
}
}
If something happens to the reader I want to log where the file the XmlTextReader is reading from. Is there an easy way to get back to the stream the XmlTextReader is using? The reason it is somewhat trivial is that I could easily pass in an additional string to the method containing the file location used to create the stream, but it just seems that has to be a way using only the XmlTextReader.
Thanks!
Update, this is actually what I'm doing... Sorry for the bad example:
public void TestMethod(XmlTextReader reader)
{
//...
//Logic
//...
if(something_inside_the_XML)
throw new Exception(FileLocation);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这个怎么样?
这应该返回创建 XmlTextReader 时使用的原始 Uri
目的。
正如MSDN所述:
How about this?
This should return the original Uri used when creating your XmlTextReader
object.
As the MSDN states:
也许你可以使用
Maybe you could use
XmlTextReader 是一次性对象,为什么不更改方法签名以便它接受文件路径,然后您可以通过 XmlTextReader 流式读取它。如果有任何错误,这将使您干净地处理阅读器并同时记录它
XmlTextReader is disposable object, why not change the method signature so that it accepts the filepath and then you can stream read it via XmlTextReader. That will make you cleanly dispose the reader if any errors and log it at the same time