Exception error with XmlTextReader

  • Thread starter Thread starter Aaron
  • Start date Start date
A

Aaron

Why am I getting the following error:

"An unhandled exception of type 'System.FormatException' occured in
mscorlib.dll"

from this code:

string[] medicationList = new string[14];
double[] parenteralValues = new double[14];
double[] oralValues = new double[14];

private void GetValues()
{
String xmlFile = "Program Files\\C-Tools\\pnmd.xml";
int arrayCount = 0;

XmlTextReader xmlTr = new XmlTextReader(xmlFile);
while (xmlTr.Read())
{
switch(xmlTr.Name)
{
case "Drug":
if (xmlTr.NodeType == XmlNodeType.Element)
medicationList[arrayCount] = xmlTr.ReadString();
break;
case "Parenteral":
if (xmlTr.NodeType == XmlNodeType.Element)
parenteralValues[arrayCount] =
System.Convert.ToDouble(xmlTr.ReadString());
break;
case "Oral":
if (xmlTr.NodeType == XmlNodeType.Element)
oralValues[arrayCount] = System.Convert.ToDouble(xmlTr.ReadString());
break;
default:
break;
}
arrayCount += 1;
}
xmlTr.Close();
}

Here's my xml structure:

<?xml version="1.0" encoding="utf-8" ?>
<Pain_Medications>
<Medications>
<Drug>Codeine</Drug>
<Parenteral>130.00</Parenteral>
<Oral>200.00</Oral>
<Comment>Codeine is usually combined with Tylenol or aspirin, and that is
the limiting factor in the dosage of this drug.
</Medications>
</Pain_Medications>

Also, if someone could tell my how to make the arrays dynamic since I won't
be having a static amount of records, the above 14 was just for testing
purposes.

Thanks
 
Aaron,

The code causing your exception is probably:
System.Convert.ToDouble(xmlTr.ReadString());
which may be failing on some string value in your XML file.

Try Double.Parse(xmlTr.ReadString()) and always put string
to numeric conversions in exception handling blocks in case the string
cannot be converted. Also a good idea to trim the string you
get out of the XML file before conversion.

Instead of a dynamic array, I'd recommend using an ArrayList
for performance reasons.

-Darren Shaffer
 
I still get the same error even though I have changed the code as you
suggested (and I am also using ArrayLists as well:
parList.Add(Double.Parse(xmlTr.ReadString().Trim()));

Darren Shaffer said:
Aaron,

The code causing your exception is probably:
System.Convert.ToDouble(xmlTr.ReadString());
which may be failing on some string value in your XML file.

Try Double.Parse(xmlTr.ReadString()) and always put string
to numeric conversions in exception handling blocks in case the string
cannot be converted. Also a good idea to trim the string you
get out of the XML file before conversion.

Instead of a dynamic array, I'd recommend using an ArrayList
for performance reasons.

-Darren Shaffer

Aaron said:
Why am I getting the following error:

"An unhandled exception of type 'System.FormatException' occured in
mscorlib.dll"

from this code:

string[] medicationList = new string[14];
double[] parenteralValues = new double[14];
double[] oralValues = new double[14];

private void GetValues()
{
String xmlFile = "Program Files\\C-Tools\\pnmd.xml";
int arrayCount = 0;

XmlTextReader xmlTr = new XmlTextReader(xmlFile);
while (xmlTr.Read())
{
switch(xmlTr.Name)
{
case "Drug":
if (xmlTr.NodeType == XmlNodeType.Element)
medicationList[arrayCount] = xmlTr.ReadString();
break;
case "Parenteral":
if (xmlTr.NodeType == XmlNodeType.Element)
parenteralValues[arrayCount] =
System.Convert.ToDouble(xmlTr.ReadString());
break;
case "Oral":
if (xmlTr.NodeType == XmlNodeType.Element)
oralValues[arrayCount] = System.Convert.ToDouble(xmlTr.ReadString());
break;
default:
break;
}
arrayCount += 1;
}
xmlTr.Close();
}

Here's my xml structure:

<?xml version="1.0" encoding="utf-8" ?>
<Pain_Medications>
<Medications>
<Drug>Codeine</Drug>
<Parenteral>130.00</Parenteral>
<Oral>200.00</Oral>
<Comment>Codeine is usually combined with Tylenol or aspirin, and that is
the limiting factor in the dosage of this drug.
</Medications>
</Pain_Medications>

Also, if someone could tell my how to make the arrays dynamic since I
won't
be having a static amount of records, the above 14 was just for testing
purposes.

Thanks
 
So like Darren said, trap the exception; sounds like
xmlTr.ReadString().Trim() does not return a number. Easiest way for you to
check that is to store the string in a local variable and examine it when
the exception occurs e.g. something like

string temp = xmlTr.ReadString().Trim();
try{
parList.Add(Double.Parse(temp));
}catch{
Debug.WriteLine(temp);
}

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


Aaron said:
I still get the same error even though I have changed the code as you
suggested (and I am also using ArrayLists as well:
parList.Add(Double.Parse(xmlTr.ReadString().Trim()));

message
Aaron,

The code causing your exception is probably:
System.Convert.ToDouble(xmlTr.ReadString());
which may be failing on some string value in your XML file.

Try Double.Parse(xmlTr.ReadString()) and always put string
to numeric conversions in exception handling blocks in case the string
cannot be converted. Also a good idea to trim the string you
get out of the XML file before conversion.

Instead of a dynamic array, I'd recommend using an ArrayList
for performance reasons.

-Darren Shaffer

Aaron said:
Why am I getting the following error:

"An unhandled exception of type 'System.FormatException' occured in
mscorlib.dll"

from this code:

string[] medicationList = new string[14];
double[] parenteralValues = new double[14];
double[] oralValues = new double[14];

private void GetValues()
{
String xmlFile = "Program Files\\C-Tools\\pnmd.xml";
int arrayCount = 0;

XmlTextReader xmlTr = new XmlTextReader(xmlFile);
while (xmlTr.Read())
{
switch(xmlTr.Name)
{
case "Drug":
if (xmlTr.NodeType == XmlNodeType.Element)
medicationList[arrayCount] = xmlTr.ReadString();
break;
case "Parenteral":
if (xmlTr.NodeType == XmlNodeType.Element)
parenteralValues[arrayCount] =
System.Convert.ToDouble(xmlTr.ReadString());
break;
case "Oral":
if (xmlTr.NodeType == XmlNodeType.Element)
oralValues[arrayCount] = System.Convert.ToDouble(xmlTr.ReadString());
break;
default:
break;
}
arrayCount += 1;
}
xmlTr.Close();
}

Here's my xml structure:

<?xml version="1.0" encoding="utf-8" ?>
<Pain_Medications>
<Medications>
<Drug>Codeine</Drug>
<Parenteral>130.00</Parenteral>
<Oral>200.00</Oral>
<Comment>Codeine is usually combined with Tylenol or aspirin, and that is
the limiting factor in the dosage of this drug.
</Medications>
</Pain_Medications>

Also, if someone could tell my how to make the arrays dynamic since I
won't
be having a static amount of records, the above 14 was just for testing
purposes.

Thanks
 
It won't let me compile that snippet cause it says "The type or namespace
'Debug' could not be found (are you missing a using directive or an assembly
reference?)

Daniel Moth said:
So like Darren said, trap the exception; sounds like
xmlTr.ReadString().Trim() does not return a number. Easiest way for you to
check that is to store the string in a local variable and examine it when
the exception occurs e.g. something like

string temp = xmlTr.ReadString().Trim();
try{
parList.Add(Double.Parse(temp));
}catch{
Debug.WriteLine(temp);
}

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


Aaron said:
I still get the same error even though I have changed the code as you
suggested (and I am also using ArrayLists as well:
parList.Add(Double.Parse(xmlTr.ReadString().Trim()));

message
Aaron,

The code causing your exception is probably:
System.Convert.ToDouble(xmlTr.ReadString());
which may be failing on some string value in your XML file.

Try Double.Parse(xmlTr.ReadString()) and always put string
to numeric conversions in exception handling blocks in case the string
cannot be converted. Also a good idea to trim the string you
get out of the XML file before conversion.

Instead of a dynamic array, I'd recommend using an ArrayList
for performance reasons.

-Darren Shaffer

Why am I getting the following error:

"An unhandled exception of type 'System.FormatException' occured in
mscorlib.dll"

from this code:

string[] medicationList = new string[14];
double[] parenteralValues = new double[14];
double[] oralValues = new double[14];

private void GetValues()
{
String xmlFile = "Program Files\\C-Tools\\pnmd.xml";
int arrayCount = 0;

XmlTextReader xmlTr = new XmlTextReader(xmlFile);
while (xmlTr.Read())
{
switch(xmlTr.Name)
{
case "Drug":
if (xmlTr.NodeType == XmlNodeType.Element)
medicationList[arrayCount] = xmlTr.ReadString();
break;
case "Parenteral":
if (xmlTr.NodeType == XmlNodeType.Element)
parenteralValues[arrayCount] =
System.Convert.ToDouble(xmlTr.ReadString());
break;
case "Oral":
if (xmlTr.NodeType == XmlNodeType.Element)
oralValues[arrayCount] = System.Convert.ToDouble(xmlTr.ReadString());
break;
default:
break;
}
arrayCount += 1;
}
xmlTr.Close();
}

Here's my xml structure:

<?xml version="1.0" encoding="utf-8" ?>
<Pain_Medications>
<Medications>
<Drug>Codeine</Drug>
<Parenteral>130.00</Parenteral>
<Oral>200.00</Oral>
<Comment>Codeine is usually combined with Tylenol or aspirin, and
that
is
the limiting factor in the dosage of this drug.
</Medications>
</Pain_Medications>

Also, if someone could tell my how to make the arrays dynamic since I
won't
be having a static amount of records, the above 14 was just for testing
purposes.

Thanks
 
You need a "using System.Diagnostics" at the top of the file or try fully
qualifying the call "System.Diagnostics.Debug.WriteLine"...

On second thoughts, if you are on a PPC, use a
"System.Windows.Forms.MessageBox.Show" to debug this particular issue... it
will be faster to do that if you have never utilised Debug.WriteLine before
(which it sounds that you haven't)

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


Aaron said:
It won't let me compile that snippet cause it says "The type or namespace
'Debug' could not be found (are you missing a using directive or an
assembly
reference?)

Daniel Moth said:
So like Darren said, trap the exception; sounds like
xmlTr.ReadString().Trim() does not return a number. Easiest way for you
to
check that is to store the string in a local variable and examine it when
the exception occurs e.g. something like

string temp = xmlTr.ReadString().Trim();
try{
parList.Add(Double.Parse(temp));
}catch{
Debug.WriteLine(temp);
}

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


Aaron said:
I still get the same error even though I have changed the code as you
suggested (and I am also using ArrayLists as well:
parList.Add(Double.Parse(xmlTr.ReadString().Trim()));

message
Aaron,

The code causing your exception is probably:
System.Convert.ToDouble(xmlTr.ReadString());
which may be failing on some string value in your XML file.

Try Double.Parse(xmlTr.ReadString()) and always put string
to numeric conversions in exception handling blocks in case the string
cannot be converted. Also a good idea to trim the string you
get out of the XML file before conversion.

Instead of a dynamic array, I'd recommend using an ArrayList
for performance reasons.

-Darren Shaffer

Why am I getting the following error:

"An unhandled exception of type 'System.FormatException' occured in
mscorlib.dll"

from this code:

string[] medicationList = new string[14];
double[] parenteralValues = new double[14];
double[] oralValues = new double[14];

private void GetValues()
{
String xmlFile = "Program Files\\C-Tools\\pnmd.xml";
int arrayCount = 0;

XmlTextReader xmlTr = new XmlTextReader(xmlFile);
while (xmlTr.Read())
{
switch(xmlTr.Name)
{
case "Drug":
if (xmlTr.NodeType == XmlNodeType.Element)
medicationList[arrayCount] = xmlTr.ReadString();
break;
case "Parenteral":
if (xmlTr.NodeType == XmlNodeType.Element)
parenteralValues[arrayCount] =
System.Convert.ToDouble(xmlTr.ReadString());
break;
case "Oral":
if (xmlTr.NodeType == XmlNodeType.Element)
oralValues[arrayCount] =
System.Convert.ToDouble(xmlTr.ReadString());
break;
default:
break;
}
arrayCount += 1;
}
xmlTr.Close();
}

Here's my xml structure:

<?xml version="1.0" encoding="utf-8" ?>
<Pain_Medications>
<Medications>
<Drug>Codeine</Drug>
<Parenteral>130.00</Parenteral>
<Oral>200.00</Oral>
<Comment>Codeine is usually combined with Tylenol or aspirin, and that
is
the limiting factor in the dosage of this drug.
</Medications>
</Pain_Medications>

Also, if someone could tell my how to make the arrays dynamic since
I
won't
be having a static amount of records, the above 14 was just for testing
purposes.

Thanks
 
Used that messagebox call and everytime through the while loop, I got an
empty string in the messagebox...how could that be?

Daniel Moth said:
You need a "using System.Diagnostics" at the top of the file or try fully
qualifying the call "System.Diagnostics.Debug.WriteLine"...

On second thoughts, if you are on a PPC, use a
"System.Windows.Forms.MessageBox.Show" to debug this particular issue... it
will be faster to do that if you have never utilised Debug.WriteLine before
(which it sounds that you haven't)

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


Aaron said:
It won't let me compile that snippet cause it says "The type or namespace
'Debug' could not be found (are you missing a using directive or an
assembly
reference?)

Daniel Moth said:
So like Darren said, trap the exception; sounds like
xmlTr.ReadString().Trim() does not return a number. Easiest way for you
to
check that is to store the string in a local variable and examine it when
the exception occurs e.g. something like

string temp = xmlTr.ReadString().Trim();
try{
parList.Add(Double.Parse(temp));
}catch{
Debug.WriteLine(temp);
}

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


I still get the same error even though I have changed the code as you
suggested (and I am also using ArrayLists as well:
parList.Add(Double.Parse(xmlTr.ReadString().Trim()));

message
Aaron,

The code causing your exception is probably:
System.Convert.ToDouble(xmlTr.ReadString());
which may be failing on some string value in your XML file.

Try Double.Parse(xmlTr.ReadString()) and always put string
to numeric conversions in exception handling blocks in case the string
cannot be converted. Also a good idea to trim the string you
get out of the XML file before conversion.

Instead of a dynamic array, I'd recommend using an ArrayList
for performance reasons.

-Darren Shaffer

Why am I getting the following error:

"An unhandled exception of type 'System.FormatException' occured in
mscorlib.dll"

from this code:

string[] medicationList = new string[14];
double[] parenteralValues = new double[14];
double[] oralValues = new double[14];

private void GetValues()
{
String xmlFile = "Program Files\\C-Tools\\pnmd.xml";
int arrayCount = 0;

XmlTextReader xmlTr = new XmlTextReader(xmlFile);
while (xmlTr.Read())
{
switch(xmlTr.Name)
{
case "Drug":
if (xmlTr.NodeType == XmlNodeType.Element)
medicationList[arrayCount] = xmlTr.ReadString();
break;
case "Parenteral":
if (xmlTr.NodeType == XmlNodeType.Element)
parenteralValues[arrayCount] =
System.Convert.ToDouble(xmlTr.ReadString());
break;
case "Oral":
if (xmlTr.NodeType == XmlNodeType.Element)
oralValues[arrayCount] =
System.Convert.ToDouble(xmlTr.ReadString());
break;
default:
break;
}
arrayCount += 1;
}
xmlTr.Close();
}

Here's my xml structure:

<?xml version="1.0" encoding="utf-8" ?>
<Pain_Medications>
<Medications>
<Drug>Codeine</Drug>
<Parenteral>130.00</Parenteral>
<Oral>200.00</Oral>
<Comment>Codeine is usually combined with Tylenol or aspirin, and that
is
the limiting factor in the dosage of this drug.
</Medications>
</Pain_Medications>

Also, if someone could tell my how to make the arrays dynamic since
I
won't
be having a static amount of records, the above 14 was just for testing
purposes.

Thanks
 
Well we solved the mysterious exception: An empty string is not a number.

The next step is to see what is wrong with your XML file or the processing
code. I suggest you use the same debugging techniques to see where you go
wrong.

If you get stuck post a small/complete sample that demonstrates the problem
(naturally your code has changed now so I cannot start there AND the
original XML you posted was not valid) so we can look at it further.

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


Aaron said:
Used that messagebox call and everytime through the while loop, I got an
empty string in the messagebox...how could that be?

Daniel Moth said:
You need a "using System.Diagnostics" at the top of the file or try fully
qualifying the call "System.Diagnostics.Debug.WriteLine"...

On second thoughts, if you are on a PPC, use a
"System.Windows.Forms.MessageBox.Show" to debug this particular issue... it
will be faster to do that if you have never utilised Debug.WriteLine before
(which it sounds that you haven't)

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


Aaron said:
It won't let me compile that snippet cause it says "The type or namespace
'Debug' could not be found (are you missing a using directive or an
assembly
reference?)

So like Darren said, trap the exception; sounds like
xmlTr.ReadString().Trim() does not return a number. Easiest way for
you
to
check that is to store the string in a local variable and examine it when
the exception occurs e.g. something like

string temp = xmlTr.ReadString().Trim();
try{
parList.Add(Double.Parse(temp));
}catch{
Debug.WriteLine(temp);
}

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


I still get the same error even though I have changed the code as you
suggested (and I am also using ArrayLists as well:
parList.Add(Double.Parse(xmlTr.ReadString().Trim()));

message
Aaron,

The code causing your exception is probably:
System.Convert.ToDouble(xmlTr.ReadString());
which may be failing on some string value in your XML file.

Try Double.Parse(xmlTr.ReadString()) and always put string
to numeric conversions in exception handling blocks in case the string
cannot be converted. Also a good idea to trim the string you
get out of the XML file before conversion.

Instead of a dynamic array, I'd recommend using an ArrayList
for performance reasons.

-Darren Shaffer

Why am I getting the following error:

"An unhandled exception of type 'System.FormatException' occured in
mscorlib.dll"

from this code:

string[] medicationList = new string[14];
double[] parenteralValues = new double[14];
double[] oralValues = new double[14];

private void GetValues()
{
String xmlFile = "Program Files\\C-Tools\\pnmd.xml";
int arrayCount = 0;

XmlTextReader xmlTr = new XmlTextReader(xmlFile);
while (xmlTr.Read())
{
switch(xmlTr.Name)
{
case "Drug":
if (xmlTr.NodeType == XmlNodeType.Element)
medicationList[arrayCount] = xmlTr.ReadString();
break;
case "Parenteral":
if (xmlTr.NodeType == XmlNodeType.Element)
parenteralValues[arrayCount] =
System.Convert.ToDouble(xmlTr.ReadString());
break;
case "Oral":
if (xmlTr.NodeType == XmlNodeType.Element)
oralValues[arrayCount] =
System.Convert.ToDouble(xmlTr.ReadString());
break;
default:
break;
}
arrayCount += 1;
}
xmlTr.Close();
}

Here's my xml structure:

<?xml version="1.0" encoding="utf-8" ?>
<Pain_Medications>
<Medications>
<Drug>Codeine</Drug>
<Parenteral>130.00</Parenteral>
<Oral>200.00</Oral>
<Comment>Codeine is usually combined with Tylenol or aspirin,
and
that
is
the limiting factor in the dosage of this drug.
</Medications>
</Pain_Medications>

Also, if someone could tell my how to make the arrays dynamic since
I
won't
be having a static amount of records, the above 14 was just for
testing
purposes.

Thanks
 
I'm not sure how the original XML I posted wasn't valid, it works in other
parts of my app where I'm doing some databinding without using the
XmlTextReader.

Daniel Moth said:
Well we solved the mysterious exception: An empty string is not a number.

The next step is to see what is wrong with your XML file or the processing
code. I suggest you use the same debugging techniques to see where you go
wrong.

If you get stuck post a small/complete sample that demonstrates the problem
(naturally your code has changed now so I cannot start there AND the
original XML you posted was not valid) so we can look at it further.

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


Aaron said:
Used that messagebox call and everytime through the while loop, I got an
empty string in the messagebox...how could that be?

Daniel Moth said:
You need a "using System.Diagnostics" at the top of the file or try fully
qualifying the call "System.Diagnostics.Debug.WriteLine"...

On second thoughts, if you are on a PPC, use a
"System.Windows.Forms.MessageBox.Show" to debug this particular
issue...
it
will be faster to do that if you have never utilised Debug.WriteLine before
(which it sounds that you haven't)

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


It won't let me compile that snippet cause it says "The type or namespace
'Debug' could not be found (are you missing a using directive or an
assembly
reference?)

So like Darren said, trap the exception; sounds like
xmlTr.ReadString().Trim() does not return a number. Easiest way for
you
to
check that is to store the string in a local variable and examine it when
the exception occurs e.g. something like

string temp = xmlTr.ReadString().Trim();
try{
parList.Add(Double.Parse(temp));
}catch{
Debug.WriteLine(temp);
}

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


I still get the same error even though I have changed the code as you
suggested (and I am also using ArrayLists as well:
parList.Add(Double.Parse(xmlTr.ReadString().Trim()));

message
Aaron,

The code causing your exception is probably:
System.Convert.ToDouble(xmlTr.ReadString());
which may be failing on some string value in your XML file.

Try Double.Parse(xmlTr.ReadString()) and always put string
to numeric conversions in exception handling blocks in case the string
cannot be converted. Also a good idea to trim the string you
get out of the XML file before conversion.

Instead of a dynamic array, I'd recommend using an ArrayList
for performance reasons.

-Darren Shaffer

Why am I getting the following error:

"An unhandled exception of type 'System.FormatException'
occured
in
mscorlib.dll"

from this code:

string[] medicationList = new string[14];
double[] parenteralValues = new double[14];
double[] oralValues = new double[14];

private void GetValues()
{
String xmlFile = "Program Files\\C-Tools\\pnmd.xml";
int arrayCount = 0;

XmlTextReader xmlTr = new XmlTextReader(xmlFile);
while (xmlTr.Read())
{
switch(xmlTr.Name)
{
case "Drug":
if (xmlTr.NodeType == XmlNodeType.Element)
medicationList[arrayCount] = xmlTr.ReadString();
break;
case "Parenteral":
if (xmlTr.NodeType == XmlNodeType.Element)
parenteralValues[arrayCount] =
System.Convert.ToDouble(xmlTr.ReadString());
break;
case "Oral":
if (xmlTr.NodeType == XmlNodeType.Element)
oralValues[arrayCount] =
System.Convert.ToDouble(xmlTr.ReadString());
break;
default:
break;
}
arrayCount += 1;
}
xmlTr.Close();
}

Here's my xml structure:

<?xml version="1.0" encoding="utf-8" ?>
<Pain_Medications>
<Medications>
<Drug>Codeine</Drug>
<Parenteral>130.00</Parenteral>
<Oral>200.00</Oral>
<Comment>Codeine is usually combined with Tylenol or aspirin,
and
that
is
the limiting factor in the dosage of this drug.
</Medications>
</Pain_Medications>

Also, if someone could tell my how to make the arrays dynamic since
I
won't
be having a static amount of records, the above 14 was just for
testing
purposes.

Thanks
 
Aaron,

On non-US devices the 130.00 may not be a valid number format. Use
Convert.ToDouble() with the explicit format provider specified:

NumberFormatInfo nfi = new NumberFormatInfo();

nfi.NumberDecimalSeparator = ".";

double val = Convert.ToDouble(xmlTr.ReadString(), nfi);
 
Where is the closing tag for Comment? As I said post a complete example that
reproduces the problem you are seeing, otherwise we are just speculating.
For example if you settings have the decimal point as a comma instead of dot
you would get a FormatException with the data you provided.

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


Aaron said:
I'm not sure how the original XML I posted wasn't valid, it works in other
parts of my app where I'm doing some databinding without using the
XmlTextReader.

Daniel Moth said:
Well we solved the mysterious exception: An empty string is not a number.

The next step is to see what is wrong with your XML file or the
processing
code. I suggest you use the same debugging techniques to see where you go
wrong.

If you get stuck post a small/complete sample that demonstrates the problem
(naturally your code has changed now so I cannot start there AND the
original XML you posted was not valid) so we can look at it further.

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


Aaron said:
Used that messagebox call and everytime through the while loop, I got
an
empty string in the messagebox...how could that be?

You need a "using System.Diagnostics" at the top of the file or try fully
qualifying the call "System.Diagnostics.Debug.WriteLine"...

On second thoughts, if you are on a PPC, use a
"System.Windows.Forms.MessageBox.Show" to debug this particular issue...
it
will be faster to do that if you have never utilised Debug.WriteLine
before
(which it sounds that you haven't)

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


It won't let me compile that snippet cause it says "The type or
namespace
'Debug' could not be found (are you missing a using directive or an
assembly
reference?)

So like Darren said, trap the exception; sounds like
xmlTr.ReadString().Trim() does not return a number. Easiest way for
you
to
check that is to store the string in a local variable and examine
it
when
the exception occurs e.g. something like

string temp = xmlTr.ReadString().Trim();
try{
parList.Add(Double.Parse(temp));
}catch{
Debug.WriteLine(temp);
}

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


I still get the same error even though I have changed the code as you
suggested (and I am also using ArrayLists as well:
parList.Add(Double.Parse(xmlTr.ReadString().Trim()));

message
Aaron,

The code causing your exception is probably:
System.Convert.ToDouble(xmlTr.ReadString());
which may be failing on some string value in your XML file.

Try Double.Parse(xmlTr.ReadString()) and always put string
to numeric conversions in exception handling blocks in case the
string
cannot be converted. Also a good idea to trim the string you
get out of the XML file before conversion.

Instead of a dynamic array, I'd recommend using an ArrayList
for performance reasons.

-Darren Shaffer

Why am I getting the following error:

"An unhandled exception of type 'System.FormatException' occured
in
mscorlib.dll"

from this code:

string[] medicationList = new string[14];
double[] parenteralValues = new double[14];
double[] oralValues = new double[14];

private void GetValues()
{
String xmlFile = "Program Files\\C-Tools\\pnmd.xml";
int arrayCount = 0;

XmlTextReader xmlTr = new XmlTextReader(xmlFile);
while (xmlTr.Read())
{
switch(xmlTr.Name)
{
case "Drug":
if (xmlTr.NodeType == XmlNodeType.Element)
medicationList[arrayCount] = xmlTr.ReadString();
break;
case "Parenteral":
if (xmlTr.NodeType == XmlNodeType.Element)
parenteralValues[arrayCount] =
System.Convert.ToDouble(xmlTr.ReadString());
break;
case "Oral":
if (xmlTr.NodeType == XmlNodeType.Element)
oralValues[arrayCount] =
System.Convert.ToDouble(xmlTr.ReadString());
break;
default:
break;
}
arrayCount += 1;
}
xmlTr.Close();
}

Here's my xml structure:

<?xml version="1.0" encoding="utf-8" ?>
<Pain_Medications>
<Medications>
<Drug>Codeine</Drug>
<Parenteral>130.00</Parenteral>
<Oral>200.00</Oral>
<Comment>Codeine is usually combined with Tylenol or aspirin,
and
that
is
the limiting factor in the dosage of this drug.
</Medications>
</Pain_Medications>

Also, if someone could tell my how to make the arrays dynamic
since
I
won't
be having a static amount of records, the above 14 was just
for
testing
purposes.

Thanks
 
I'm also noticing that actual strings in the XML are coming back empty as
well...this just isn't making sense...

Daniel Moth said:
Well we solved the mysterious exception: An empty string is not a number.

The next step is to see what is wrong with your XML file or the processing
code. I suggest you use the same debugging techniques to see where you go
wrong.

If you get stuck post a small/complete sample that demonstrates the problem
(naturally your code has changed now so I cannot start there AND the
original XML you posted was not valid) so we can look at it further.

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


Aaron said:
Used that messagebox call and everytime through the while loop, I got an
empty string in the messagebox...how could that be?

Daniel Moth said:
You need a "using System.Diagnostics" at the top of the file or try fully
qualifying the call "System.Diagnostics.Debug.WriteLine"...

On second thoughts, if you are on a PPC, use a
"System.Windows.Forms.MessageBox.Show" to debug this particular
issue...
it
will be faster to do that if you have never utilised Debug.WriteLine before
(which it sounds that you haven't)

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


It won't let me compile that snippet cause it says "The type or namespace
'Debug' could not be found (are you missing a using directive or an
assembly
reference?)

So like Darren said, trap the exception; sounds like
xmlTr.ReadString().Trim() does not return a number. Easiest way for
you
to
check that is to store the string in a local variable and examine it when
the exception occurs e.g. something like

string temp = xmlTr.ReadString().Trim();
try{
parList.Add(Double.Parse(temp));
}catch{
Debug.WriteLine(temp);
}

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


I still get the same error even though I have changed the code as you
suggested (and I am also using ArrayLists as well:
parList.Add(Double.Parse(xmlTr.ReadString().Trim()));

message
Aaron,

The code causing your exception is probably:
System.Convert.ToDouble(xmlTr.ReadString());
which may be failing on some string value in your XML file.

Try Double.Parse(xmlTr.ReadString()) and always put string
to numeric conversions in exception handling blocks in case the string
cannot be converted. Also a good idea to trim the string you
get out of the XML file before conversion.

Instead of a dynamic array, I'd recommend using an ArrayList
for performance reasons.

-Darren Shaffer

Why am I getting the following error:

"An unhandled exception of type 'System.FormatException'
occured
in
mscorlib.dll"

from this code:

string[] medicationList = new string[14];
double[] parenteralValues = new double[14];
double[] oralValues = new double[14];

private void GetValues()
{
String xmlFile = "Program Files\\C-Tools\\pnmd.xml";
int arrayCount = 0;

XmlTextReader xmlTr = new XmlTextReader(xmlFile);
while (xmlTr.Read())
{
switch(xmlTr.Name)
{
case "Drug":
if (xmlTr.NodeType == XmlNodeType.Element)
medicationList[arrayCount] = xmlTr.ReadString();
break;
case "Parenteral":
if (xmlTr.NodeType == XmlNodeType.Element)
parenteralValues[arrayCount] =
System.Convert.ToDouble(xmlTr.ReadString());
break;
case "Oral":
if (xmlTr.NodeType == XmlNodeType.Element)
oralValues[arrayCount] =
System.Convert.ToDouble(xmlTr.ReadString());
break;
default:
break;
}
arrayCount += 1;
}
xmlTr.Close();
}

Here's my xml structure:

<?xml version="1.0" encoding="utf-8" ?>
<Pain_Medications>
<Medications>
<Drug>Codeine</Drug>
<Parenteral>130.00</Parenteral>
<Oral>200.00</Oral>
<Comment>Codeine is usually combined with Tylenol or aspirin,
and
that
is
the limiting factor in the dosage of this drug.
</Medications>
</Pain_Medications>

Also, if someone could tell my how to make the arrays dynamic since
I
won't
be having a static amount of records, the above 14 was just for
testing
purposes.

Thanks
 
My apologies then, my original XML file does indeed have a closing tag for
every comment.

Daniel Moth said:
Where is the closing tag for Comment? As I said post a complete example that
reproduces the problem you are seeing, otherwise we are just speculating.
For example if you settings have the decimal point as a comma instead of dot
you would get a FormatException with the data you provided.

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


Aaron said:
I'm not sure how the original XML I posted wasn't valid, it works in other
parts of my app where I'm doing some databinding without using the
XmlTextReader.

Daniel Moth said:
Well we solved the mysterious exception: An empty string is not a number.

The next step is to see what is wrong with your XML file or the
processing
code. I suggest you use the same debugging techniques to see where you go
wrong.

If you get stuck post a small/complete sample that demonstrates the problem
(naturally your code has changed now so I cannot start there AND the
original XML you posted was not valid) so we can look at it further.

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


Used that messagebox call and everytime through the while loop, I got
an
empty string in the messagebox...how could that be?

You need a "using System.Diagnostics" at the top of the file or try fully
qualifying the call "System.Diagnostics.Debug.WriteLine"...

On second thoughts, if you are on a PPC, use a
"System.Windows.Forms.MessageBox.Show" to debug this particular issue...
it
will be faster to do that if you have never utilised Debug.WriteLine
before
(which it sounds that you haven't)

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


It won't let me compile that snippet cause it says "The type or
namespace
'Debug' could not be found (are you missing a using directive or an
assembly
reference?)

So like Darren said, trap the exception; sounds like
xmlTr.ReadString().Trim() does not return a number. Easiest way for
you
to
check that is to store the string in a local variable and examine
it
when
the exception occurs e.g. something like

string temp = xmlTr.ReadString().Trim();
try{
parList.Add(Double.Parse(temp));
}catch{
Debug.WriteLine(temp);
}

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


I still get the same error even though I have changed the code
as
you
suggested (and I am also using ArrayLists as well:
parList.Add(Double.Parse(xmlTr.ReadString().Trim()));

"Darren Shaffer" <[email protected]>
wrote
in
message
Aaron,

The code causing your exception is probably:
System.Convert.ToDouble(xmlTr.ReadString());
which may be failing on some string value in your XML file.

Try Double.Parse(xmlTr.ReadString()) and always put string
to numeric conversions in exception handling blocks in case the
string
cannot be converted. Also a good idea to trim the string you
get out of the XML file before conversion.

Instead of a dynamic array, I'd recommend using an ArrayList
for performance reasons.

-Darren Shaffer

Why am I getting the following error:

"An unhandled exception of type 'System.FormatException' occured
in
mscorlib.dll"

from this code:

string[] medicationList = new string[14];
double[] parenteralValues = new double[14];
double[] oralValues = new double[14];

private void GetValues()
{
String xmlFile = "Program Files\\C-Tools\\pnmd.xml";
int arrayCount = 0;

XmlTextReader xmlTr = new XmlTextReader(xmlFile);
while (xmlTr.Read())
{
switch(xmlTr.Name)
{
case "Drug":
if (xmlTr.NodeType == XmlNodeType.Element)
medicationList[arrayCount] = xmlTr.ReadString();
break;
case "Parenteral":
if (xmlTr.NodeType == XmlNodeType.Element)
parenteralValues[arrayCount] =
System.Convert.ToDouble(xmlTr.ReadString());
break;
case "Oral":
if (xmlTr.NodeType == XmlNodeType.Element)
oralValues[arrayCount] =
System.Convert.ToDouble(xmlTr.ReadString());
break;
default:
break;
}
arrayCount += 1;
}
xmlTr.Close();
}

Here's my xml structure:

<?xml version="1.0" encoding="utf-8" ?>
<Pain_Medications>
<Medications>
<Drug>Codeine</Drug>
<Parenteral>130.00</Parenteral>
<Oral>200.00</Oral>
<Comment>Codeine is usually combined with Tylenol or aspirin,
and
that
is
the limiting factor in the dosage of this drug.
</Medications>
</Pain_Medications>

Also, if someone could tell my how to make the arrays dynamic
since
I
won't
be having a static amount of records, the above 14 was just
for
testing
purposes.

Thanks
 
Back
Top