Exception thrown outside debugger but not in debugger

G

Guest

Hi,

I am experiencing a strange problem. I am reading and writing xml files via
XmlDocument and XmlTextWriter. In the debugger everything works fine but
outside the debugger I receive the following error: "The
type initializer for "System.Xml.Schema.Validator" threw an exception.".
When running from the debugger (debug or release) no exception is thrown.

I wrote a small console app that replicates the problem -- I've just attached
the default class which gets run. Output outside the debugger is as follows

------------------
Creating xml file
Reading xml file
Error reading xml file
The type initializer for "System.Xml.Schema.Validator" threw an exception.

Press a key to exit
------------------

I know that this code worked at some point in time and I do not get the
error message when running debug or release from the debugger. Runnning
WinXP SP2 and Visual Studio .Net 2003 (C#). Please feel free to correct me
if I am doing something incorrectly or let me know if it runs error free for
you.

Thanks in advance,
Chris

<snip>
using System;
using System.IO;
using System.Xml;

namespace Test
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class XmlProblem
{
private static string m_sXmlFile = @"C:\temp\test.xml";

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: Add code to start application here
//

CreateXMLFile();
ReadXMLFile();
Console.WriteLine("");
Console.WriteLine("Press a key to exit");
Console.ReadLine();
}

static void CreateXMLFile()
{
StreamWriter sw = null;

try
{
Console.WriteLine("Creating xml file");

sw = new StreamWriter(m_sXmlFile, false);
XmlTextWriter xWriter = new XmlTextWriter(sw);
xWriter.Formatting = Formatting.Indented;
xWriter.Indentation = 3;
xWriter.WriteStartDocument();
xWriter.WriteStartElement("ApplicationSettings");
xWriter.WriteEndElement(); // AppSettings
xWriter.WriteEndDocument();
xWriter.Flush();
xWriter.Close();
}
catch(Exception ex)
{
Console.WriteLine("Error creating xml file " + ex.Message);
}

if (sw != null)
sw.Close();
}

static void ReadXMLFile()
{
StreamReader sr = null;

try
{
Console.WriteLine("Reading xml file");

sr = new StreamReader(m_sXmlFile);
string sFileContents = sr.ReadToEnd();

XmlDocument xDoc = new XmlDocument();
xDoc.LoadXml(sFileContents);
}
catch(Exception ex)
{
Console.WriteLine("Error reading xml file");
Console.WriteLine(ex.Message);
}

if (sr != null)
sr.Close();
}

}
}

</snip>
 
A

Alvin Bruney - ASP.NET MVP

I ran your code under the debugger, then in release mode. No errors. Set the
debugger to break on all exceptions, No errors.

--
Warm Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
The Microsoft Office Web Components Black Book with .NET
Now Available @ www.lulu.com/owc
Professional VSTO 2005 - Wrox/Wiley 2006
Blog: http://msmvps.com/blogs/Alvin/
-------------------------------------------------------



Chris Stiefeling said:
Hi,

I am experiencing a strange problem. I am reading and writing xml files via
XmlDocument and XmlTextWriter. In the debugger everything works fine but
outside the debugger I receive the following error: "The
type initializer for "System.Xml.Schema.Validator" threw an exception.".
When running from the debugger (debug or release) no exception is thrown.

I wrote a small console app that replicates the problem -- I've just attached
the default class which gets run. Output outside the debugger is as follows

------------------
Creating xml file
Reading xml file
Error reading xml file
The type initializer for "System.Xml.Schema.Validator" threw an exception.

Press a key to exit
------------------

I know that this code worked at some point in time and I do not get the
error message when running debug or release from the debugger. Runnning
WinXP SP2 and Visual Studio .Net 2003 (C#). Please feel free to correct me
if I am doing something incorrectly or let me know if it runs error free for
you.

Thanks in advance,
Chris

<snip>
using System;
using System.IO;
using System.Xml;

namespace Test
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class XmlProblem
{
private static string m_sXmlFile = @"C:\temp\test.xml";

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: Add code to start application here
//

CreateXMLFile();
ReadXMLFile();
Console.WriteLine("");
Console.WriteLine("Press a key to exit");
Console.ReadLine();
}

static void CreateXMLFile()
{
StreamWriter sw = null;

try
{
Console.WriteLine("Creating xml file");

sw = new StreamWriter(m_sXmlFile, false);
XmlTextWriter xWriter = new XmlTextWriter(sw);
xWriter.Formatting = Formatting.Indented;
xWriter.Indentation = 3;
xWriter.WriteStartDocument();
xWriter.WriteStartElement("ApplicationSettings");
xWriter.WriteEndElement(); // AppSettings
xWriter.WriteEndDocument();
xWriter.Flush();
xWriter.Close();
}
catch(Exception ex)
{
Console.WriteLine("Error creating xml file " + ex.Message);
}

if (sw != null)
sw.Close();
}

static void ReadXMLFile()
{
StreamReader sr = null;

try
{
Console.WriteLine("Reading xml file");

sr = new StreamReader(m_sXmlFile);
string sFileContents = sr.ReadToEnd();

XmlDocument xDoc = new XmlDocument();
xDoc.LoadXml(sFileContents);
}
catch(Exception ex)
{
Console.WriteLine("Error reading xml file");
Console.WriteLine(ex.Message);
}

if (sr != null)
sr.Close();
}

}
}

</snip>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top