将 XmlReader 转换为 XmlTextReader
如何将 XmlReader 转换为 XmlTextReader?
代码片段:
XmlTextReader reader = XmlTextReader.Create(pomfile.FullName);
这是我得到的构建错误:
Cannot implicitly convert type 'System.Xml.XmlReader' to 'System.Xml.XmlTextReader'. An
存在显式转换(您是否缺少强制转换?)。
pomfile 的类型为 FileInfo
How do you convert XmlReader to XmlTextReader?
Code Snippet:
XmlTextReader reader = XmlTextReader.Create(pomfile.FullName);
Here's the Build error I got:
Cannot implicitly convert type 'System.Xml.XmlReader' to 'System.Xml.XmlTextReader'. An
explicit conversion exists(are you missing a cast?).
pomfile is of type FileInfo
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
XmlTextReader.Create()
函数生成 XMLReader,您必须将其转换为XmlTextReader
,但是如果转换不可能,这可能会产生运行时异常:或者您可以这样做:
但最好要做的事情是:
XmlTextReader.Create()
function produces XMLReader that you have to cast toXmlTextReader
but this can produce runtime exception if the cast is impossible:or you can do this:
but the best thing to do is:
XmlTextReader
在 .NET 2.0 中已过时。只需这样做:XmlTextReader
is obsolete in .NET 2.0. Just do this instead:XmlReader
是XmlTextReader
的抽象基类,因此您需要强制向下转型(我不建议这样做)。直接实例化您期望的类(如 najmeddine 的回答中指出的)
XmlReader
is the abstract base class ofXmlTextReader
so you would need to force a downcast (which I would not advise).Instantiate the class you are expecting directly (as pointed out in najmeddine's answer)