Re: Xml validation exception only occurs outside debugger - VS 200



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..cctor()
--- End of inner exception stack trace ---
at System.Text.RegularExpressions.RegexCharClass..ctor()
at System.Text.RegularExpressions.RegexParser.ScanCharClass(Boolean
caseInsen
sitive, Boolean scanOnly)
at System.Text.RegularExpressions.RegexParser.ScanRegex()
at System.Text.RegularExpressions.RegexParser.Parse(String 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.FromXmlTokenizedType(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(XmlTextReader 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" <ChrisStiefeling@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in
message news:0CFA31CE-609F-4C12-B0FA-E2577C95B5A9@xxxxxxxxxxxxxxxx
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>



.



Relevant Pages

  • Re: Exception thrown outside debugger but not in debugger
    ... I ran your code under the debugger, ... Creating xml file ... Error reading xml file ... The type initializer for "System.Xml.Schema.Validator" threw an exception. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Xml validation exception only occurs outside debugger - VS 2003 C#
    ... Could you attach the entire stack trace and message for the exception? ... outside the debugger I receive the following error: ... Creating xml file ... Error reading xml file ...
    (microsoft.public.dotnet.xml)
  • RE: Problem with LWP::UserAgent
    ... have an xml file that contains a list of URLs. ... hash and pass them as a parameter into the function that does LWP get. ... > assume your script is named net.pl, you can involve the debugger with: ...
    (perl.beginners)
  • Re: default namespace / Xmlvalidatingreader
    ... You dont get an exception in the case of the second file, ... validation warning should be thrown. ... XmlReader readerToUse = validatingReader; ...
    (microsoft.public.dotnet.xml)
  • Re: XmlValidatingReader / Default Namespace
    ... You dont get an exception in the case of the second file, ... validation warning should be thrown. ... XmlReader readerToUse = validatingReader; ...
    (microsoft.public.dotnet.xml)