static void ReadXml()
{
string a= null;
double b= 0;
double c= 0;
XmlReader xmlReader = new XmlReader("Testxml.xml");
xmlReader.
using (xmlReader)
{
if (xmlReader != null)
{
while (xmlReader.Read())
{
if (xmlReader.NodeType == XmlNodeType.Element)
{
switch (xmlReader.Name)
{
case "a":
a = xmlReader.ReadElementContentAsString();
break;
case "b":
b = double.Parse(xmlReader.ReadElementContentAsString());
break;
case "c":
c = double.Parse(xmlReader.ReadElementContentAsString());
break;
}
}
}
}
}
}
TestXML content:
<a><b>26a83f12c782</b><c>128</c><d>12</d></a>
Case b is never hit. But If I add a space after end element of b, case b is hit. Now how to make it work without changing the xml file?
Here's a working version of your code. The specific problems that are corrected include:
new XmlReader
doesn't compile. It's an abstract class. You need to useXmlTextReader
or anotherXmlReader
derived class.b is not a valid double. You were trying to convert a large hex number to a double directly which isn't possible. You can use
NumberStyles.HexNumber
in theParse
call, but not withdouble
, it has to belong
orint
.Double read. You were calling
Read()
inside a loop but then using theXmlReader.ReadXxx()
methods as well. This was calling read extra times and skipping nodes. This is really the main problem you were asking about. The following code keeps track of the last element found and then waits till it hits theText
node for processing. This is fine for simple/flat documents, but for more complex ones you need a better way of keeping track of state, like a finite state machine. Or use DOM. Or LINQ.