Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / XML / April 2006

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Chris Stiefeling - 09 Mar 2006 18:26 GMT
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>
Amol Kher [MSFT] - 11 Apr 2006 19:42 GMT
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

> Hi,
>
[quoted text clipped - 117 lines]
>
> </snip>
Chris Stiefeling - 28 Apr 2006 17:03 GMT
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

> Chris,
>
[quoted text clipped - 128 lines]
> >
> > </snip>

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.