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>
.



Relevant Pages

  • Re: Collecting multiple element names using grouptag
    ... I have an XML file that I am reading and I am using XML::Simple to ... read the posting guidelines for this group. ...
    (comp.lang.perl.misc)
  • Re: What makes VB.Net XslTransform REALLY REALLY SLOW?
    ... > situations with heavy dependency on the xsl:key function. ... Thanks - I cam across the xsl:key references on google. ... next/previous/child/parent (i.e. reading databases)- but is slow beyond ... The only thing I am using .Net for is reading a large XML file and ...
    (microsoft.public.dotnet.framework.performance)
  • Re: Read XML value
    ... Chris, I'd suggest you can use the XSD.EXE tool that comes with the .net ... You can edit the schema to make sure it is what you want. ... > Below is an XML file I have been given. ... > just cannot work out the code for reading a value. ...
    (microsoft.public.dotnet.general)
  • Using static library
    ... I needed a library for reading ... the configuration from an xml file. ... then we rebuilt the static library after ...
    (microsoft.public.vc.language)
  • Exception thrown outside debugger but not in debugger
    ... I am reading and writing xml files via ... XmlDocument and XmlTextWriter. ... outside the debugger I receive the following error: ... Creating xml file ...
    (microsoft.public.dotnet.languages.csharp)