XmlReader problem (C# / VB)
-
Hi All, I am sure the solution to this is *Really* easy, but i've been staring at it for a while now and have got nowhere. I've written the test below in both C# and VB and both do the same thing. I need to use an XmlReader object to read in an xml document. Note that it is essential that i use an XmlReader object as I need to perform schema validation. The code below does not incluse schema validation as as this stage i'm just trying to get an xml file into an XmlReader. Looking in the msdn and a range of examples in google i should be able to get an XmlReader object containing my xml document by using XmlReader.Create(TextReader o). Everything is working right up until that step, but using the debug tools my XmlReader object appears to remain content-less. Here's the code: using System; using System.Collections.Generic; using System.Text; using System.Xml; using System.IO; namespace XmlTest { class Program { static void Main(string[] args) { String file = "C:\\WINDOWS\\Microsoft.NET\\Framework\\v2.0.50727\\CONFIG\\web.config"; XmlDocument doc = new XmlDocument(); doc.Load(new System.IO.StreamReader(file)); String docString = doc.OuterXml; TextReader tr = new StringReader(docString); XmlReader xr = XmlReader.Create(tr); System.Console.ReadKey(); } } } So we're loading in an xml file, putting it into an XmlDocument and getting the Xml as a string out of it. This is because ultimately when i slot this work back into what i'm doing i'll be obtaining the xml document via a string. A TextReader object is then created from the string via a StringReader. If i debug the code i can see the document correctly loaded into the TextReader (tr) in it's entirity. It just doesn't seem to be converted into an XmlReader object via XmlReader.Create(tr). Similarly, if i replace XmlReader.Create(tr) with XmlReader.Create(new System.IO.StreamReader(file)) I have the same problem, proving it's none of the logic inbetween that is messing things up. What am i missing about XmlReader? Please help if you can, this is driving me absolutely nuts. When i look at it in debug the object has a depth of zero and looking in items i see "In order to evaluate an indexed property, the property must be qualified and the arguments must be explicitly supplied by the user." I have no idea what this means and google doesn't help. You should be able to copy and paste the cod
-
Hi All, I am sure the solution to this is *Really* easy, but i've been staring at it for a while now and have got nowhere. I've written the test below in both C# and VB and both do the same thing. I need to use an XmlReader object to read in an xml document. Note that it is essential that i use an XmlReader object as I need to perform schema validation. The code below does not incluse schema validation as as this stage i'm just trying to get an xml file into an XmlReader. Looking in the msdn and a range of examples in google i should be able to get an XmlReader object containing my xml document by using XmlReader.Create(TextReader o). Everything is working right up until that step, but using the debug tools my XmlReader object appears to remain content-less. Here's the code: using System; using System.Collections.Generic; using System.Text; using System.Xml; using System.IO; namespace XmlTest { class Program { static void Main(string[] args) { String file = "C:\\WINDOWS\\Microsoft.NET\\Framework\\v2.0.50727\\CONFIG\\web.config"; XmlDocument doc = new XmlDocument(); doc.Load(new System.IO.StreamReader(file)); String docString = doc.OuterXml; TextReader tr = new StringReader(docString); XmlReader xr = XmlReader.Create(tr); System.Console.ReadKey(); } } } So we're loading in an xml file, putting it into an XmlDocument and getting the Xml as a string out of it. This is because ultimately when i slot this work back into what i'm doing i'll be obtaining the xml document via a string. A TextReader object is then created from the string via a StringReader. If i debug the code i can see the document correctly loaded into the TextReader (tr) in it's entirity. It just doesn't seem to be converted into an XmlReader object via XmlReader.Create(tr). Similarly, if i replace XmlReader.Create(tr) with XmlReader.Create(new System.IO.StreamReader(file)) I have the same problem, proving it's none of the logic inbetween that is messing things up. What am i missing about XmlReader? Please help if you can, this is driving me absolutely nuts. When i look at it in debug the object has a depth of zero and looking in items i see "In order to evaluate an indexed property, the property must be qualified and the arguments must be explicitly supplied by the user." I have no idea what this means and google doesn't help. You should be able to copy and paste the cod
I think the problem is that the reader isn't position on an XML node after being created, so you'll have to call any Read method before the properties (e.g. depth) return any useful information.
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook
-
I think the problem is that the reader isn't position on an XML node after being created, so you'll have to call any Read method before the properties (e.g. depth) return any useful information.
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook
Thanks Stefan, that was the problem. I'd been expecting the debug fields to populate as soon as the object had been created but as you say, running a ReadToEnd populated the fields. Thanks, Rolf