By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,084 Members | 1,233 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,084 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.