convert XmlReader to XmlTextReader

user186222 picture user186222 · Oct 8, 2009 · Viewed 7k times · Source

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

Answer

manji picture manji · Oct 8, 2009

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);