重构XmlTextWriter,偏执的问题 [英] Refactoring XmlTextWriter, paranoid question

查看:107
本文介绍了重构XmlTextWriter,偏执的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在重构以下代码:

I''m refactoring the following code:

using (StreamReader stream = new StreamReader(InputFilePath))
{
    XmlTextReader xmlIn = new XmlTextReader(stream);
    while (xmlIn.Read())
    {
        //Do stuff
    }
    xmlIn.Close();
}



首先,我有点偏执,想检查以下内容是否会起作用,因为我不确定为什么原来没有这样做:



First I''m, being a bit paranoid and want to check if the following will behave, as I''m unsure why this wasn''t done in the originally:

using (XmlTextReader xmlIn = new XmlTextReader(new StreamReader(inputFilePath)))
{
    while (xmlIn.Read())
    {
        //Do stuff
    }
}



其次,我也很清楚地记得,有一种比新的XmlTextReader(new StreamReader(inputFilePath))
更加整洁的方法. 实例化,如果我没错,建议会很有用.

谢谢.



Secondly, I also vauguely remember there is a neater way than the new XmlTextReader(new StreamReader(inputFilePath))
instantiation, if I''m right suggestions would be useful.

Thanks.

推荐答案

我很想保留它的原样.
是否在XmlTextReader上配置也配置流?我知道XmlTextReader(通过XmlReader)实现IDisposable,但我认为使用第一个版本处理流(这是重要的一点)更加清楚.
I would be tempted to leave it as it is.
Does dispose on the XmlTextReader also dispose the stream? I know XmlTextReader implements IDisposable (via XmlReader), but I think it is clearer that the stream is disposed (which is the important bit) with the first version.


这篇关于重构XmlTextWriter,偏执的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆