开发者

convert XmlReader to XmlTextReader

开发者 https://www.devze.com 2022-12-08 04:20 出处:网络
How do you convert XmlReader to XmlTextReader? Code Snippet: XmlTextReader reader = XmlTextReader.Create(pomfile.FullName);

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开发者_Go百科.Xml.XmlTextReader'. An

explicit conversion exists(are you missing a cast?).

pomfile is of type FileInfo


XmlTextReader.Create() function produces XMLReader that you have to cast to XmlTextReader but this can produce runtime exception if the cast is impossible:

XmlTextReader tr = (XmlTextReader)XmlTextReader.Create(pomfile.FullName));

or you can do this:

XmlTextReader reader = new XmlTextReader(XmlTextReader.Create(pomfile.FullName));

but the best thing to do is:

XmlTextReader reader = new XmlTextReader(pomfile.FullName);


XmlTextReader is obsolete in .NET 2.0. Just do this instead:

XmlReader reader = XmlReader.Create(pomfile.FullName);


XmlReader is the abstract base class of XmlTextReader 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)

0

精彩评论

暂无评论...
验证码 换一张
取 消