Hi, I am having some problems with the XmlTextReader class. In the code [1] below, I have two Methods 'Run' and 'GetXml' apart from the Main. The 'GetXml' Method reads an xml file [3], read a couple of nodes from the xml and returns the XmlTextReader object. The returned XmlTextReader object is then passed to the Run Method, which also does the same iteration over the nodes. See the source [1]
The confusion begins when I saw the output [2] which demonstrate that the XmlTextReader object in the 'Run' Method starts reading from where the XmlTextReader object left in the 'GetXml' Method. Is there any way I can reset the state of the XmlTextReader object in the 'Run' Method? Or some other solutions may be: a) I have to read the xml file again in the 'Run' Method? b) XmlDocument should be used instead of XmlTextReader in the 'GetXml' Method? -maybe there is something very simple that I am missing here /noman --------------------------------------------- source -------------- [1] using System; using System.Xml; public class MainClass { public static void Main() { MainClass mClass = new MainClass(); XmlTextReader tempReader = mClass.GetXml(); mClass.Run(tempReader); } // end of Main public XmlTextReader GetXml() { Console.WriteLine("Inside GetXml() ---------------"); XmlTextReader xmlReader = new XmlTextReader("books.xml"); xmlReader.Read(); Console.WriteLine(xmlReader.Name); Console.WriteLine(xmlReader.Value); xmlReader.Read(); Console.WriteLine(xmlReader.Name); Console.WriteLine(xmlReader.Value); xmlReader.Read(); Console.WriteLine(xmlReader.Name); Console.WriteLine(xmlReader.Value); return xmlReader; } // end of GetXml void Run(XmlTextReader tempReader) { Console.WriteLine("Inside Run() ---------------"); tempReader.ResetState(); tempReader.Read(); Console.WriteLine(tempReader.Name); Console.WriteLine(tempReader.Value); tempReader.Read(); Console.WriteLine(tempReader.Name); Console.WriteLine(tempReader.Value); tempReader.Read(); Console.WriteLine(tempReader.Name); Console.WriteLine(tempReader.Value); } // End of Run } // end of MainClass -------------------------------------- end source ----------- -------------------------------------- xml file ------------ [3] <?xml version = "1.0"?> <books> <book> <title>.NET XML Programming</title> <author> <firstname>Steve</firstname> <lastname>James</lastname> </author> <chapters> <chapter num = "1" pages = "10">Introduction to XML Programming with .NET Framework</chapter> <chapter num = "2" pages = "32">Parsing XML</chapter> </chapters> </book> </books> -------------------------------------- end xml file -------- -------------------------------------- output -------------- [2] Inside GetXml() --------------- xml version = "1.0" books Inside Run() --------------- book -------------------------------------- end output ----------- You can read messages from the Advanced DOTNET archive, unsubscribe from Advanced DOTNET, or subscribe to other DevelopMentor lists at http://discuss.develop.com.