473,383 Members | 1,868 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Xml validation exception only occurs outside debugger - VS 2003 C#

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 (debug or release) I receive the following error: "The
type initializer for "System.Xml.Schema.Validator" threw an exception."

I wrote a small console app that contains 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>
Mar 9 '06 #1
2 2859
Chris,

I tried to repro your issue, but I couldnt. I copied your code, compiled it
and ran it on 1.1 with XP SP2, outside of vc# 2003.

Could you attach the entire stack trace and message for the exception? Dont
catch the exception and let it show up on the screen.

Thanks,
Amol
"Chris Stiefeling" <Ch*************@discussions.microsoft.com> wrote in
message news:0C**********************************@microsof t.com...
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 (debug or release) I receive the following error:
"The
type initializer for "System.Xml.Schema.Validator" threw an exception."

I wrote a small console app that contains 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>

Apr 11 '06 #2
Thanks for your reply -- I almost missed it until I went searching on Google
for the same problem again.

I'm able to reproduce the problem on any Xml file (even ones produced by
MSFT programs). Looks like the source is actually a null reference in the
regex classes.

I can also send along my exe if it is helpful.

Here's the stack trace:

Unhandled Exception: System.TypeInitializationException: The type
initializer fo
r "System.Xml.Schema.Validator" threw an exception. --->
System.TypeInitializati
onException: The type initializer for
"System.Xml.Schema.DatatypeImplementation"
threw an exception. ---> System.TypeInitializationException: The type
initializ
er for "System.Xml.Schema.Datatype_language" threw an exception. --->
System.Typ
eInitializationException: The type initializer for
"System.Text.RegularExpressio
ns.RegexCharClass" threw an exception. ---> System.NullReferenceException:
Objec
t reference not set to an instance of an object.
at System.Text.RegularExpressions.RegexCharClass..cct or()
--- End of inner exception stack trace ---
at System.Text.RegularExpressions.RegexCharClass..cto r()
at System.Text.RegularExpressions.RegexParser.ScanCha rClass(Boolean
caseInsen
sitive, Boolean scanOnly)
at System.Text.RegularExpressions.RegexParser.ScanReg ex()
at System.Text.RegularExpressions.RegexParser.Parse(S tring re,
RegexOptions o
p)
at System.Text.RegularExpressions.Regex..ctor(String pattern,
RegexOptions op
tions)
at System.Xml.Schema.Datatype_language..cctor()
--- End of inner exception stack trace ---
at System.Xml.Schema.Datatype_language..ctor()
at System.Xml.Schema.DatatypeImplementation..cctor()
--- End of inner exception stack trace ---
at
System.Xml.Schema.XmlSchemaDatatype.FromXmlTokeniz edType(XmlTokenizedType
token)
at System.Xml.Schema.Validator..cctor()
--- End of inner exception stack trace ---
at System.Xml.Schema.Validator..ctor(XmlNameTable nameTable,
XmlValidatingRea
der reader)
at System.Xml.XmlValidatingReader..ctor(XmlReader reader)
at System.Xml.XmlDocument.CreateValidatingReader(XmlT extReader tr)
at System.Xml.XmlDocument.LoadXml(String xml)
at Test.XmlProblem.ReadXMLFile()
at Test.XmlProblem.Main(String[] args)

Thanks for your help.

Chris

"Amol Kher [MSFT]" wrote:
Chris,

I tried to repro your issue, but I couldnt. I copied your code, compiled it
and ran it on 1.1 with XP SP2, outside of vc# 2003.

Could you attach the entire stack trace and message for the exception? Dont
catch the exception and let it show up on the screen.

Thanks,
Amol
"Chris Stiefeling" <Ch*************@discussions.microsoft.com> wrote in
message news:0C**********************************@microsof t.com...
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 (debug or release) I receive the following error:
"The
type initializer for "System.Xml.Schema.Validator" threw an exception."

I wrote a small console app that contains 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>


Apr 28 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Noor | last post by:
please tell the technique of centralize exception handling without try catch blocks in c#.
0
by: Diana Bulthaupt | last post by:
Hello, have implemented a global exception handler. If an exception occurs, the event OnThreadException will be called. In this method I call Debugger.Launch() to start a Debugger. This works so...
5
by: Peter Steele | last post by:
We have an application that when it runs in the IDE in debug mode an unhandled exception is occurring in a system header file associated with STL stirngs. The actual statement that crashes is ...
9
by: RalphTheExpert | last post by:
I'm getting different behavior if my code is running under the debugger or not. I have modified Winmain to look like this: // Copyright (C) 2002 Microsoft Corporation // All rights reserved....
5
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS...
9
by: Claudio Di Flumeri | last post by:
Hello all, I've added a global exception handler to my application in this way: Sub Main() AddHandler Application.ThreadException, AddressOf ThreadException AddHandler...
5
by: Samuel R. Neff | last post by:
When you have an unhandled exception in vb.net how do you view the exception information in the debugger? In C# the debugger creates a local variable that points to the exception and you can...
1
by: Chris Stiefeling | last post by:
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...
5
by: pcnerd | last post by:
I'm trying to create a program that plots randomly colored pixels on a bitmap & then displays the bitmap. When I run the program, I see the pixels being plotted down the left side of the form. When...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.