M
Mark Winters
I have code that, up until recently, was working fine but now is
failing with the following message:
An unhandled exception of type 'System.InvalidOperationException'
occurred in system.xml.dll
Additional information: There is an error in XML document (15, 3).
The statement it is failing on is below:
FileVariablesCollection =
(FileVariablesCollection)ser.Deserialize(reader);
The xml document being read is below:
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfFileVariables xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<FileVariables>
<UserID>mark.r.wingfield</UserID>
<FileName>Test Script name 4.sql</FileName>
<FileType>Template</FileType>
<TemplateFolder>c:\template</TemplateFolder>
<ScriptFolder>c:\script</ScriptFolder>
<ResultsFolder>c:\results</ResultsFolder>
<SavedScriptPrefix>PREFIXY stuff</SavedScriptPrefix>
<SRPRGCARNo>SR222222A</SRPRGCARNo>
<ResultsFileType>Template</ResultsFileType>
<SavedResultsSuffix>.csv</SavedResultsSuffix>
</FileVariables>
</ArrayOfFileVariables>
The full code for the failing method is:
public static FileVariablesCollection GetFileVariables()
{
FileVariablesCollection FileVariablesCollection;
string filename =
Path.Combine(Path.GetDirectoryName(Directory.GetCurrentDirectory()),
configFile);
if(!System.IO.File.Exists(filename))
return (new FileVariablesCollection());
XmlSerializer ser = new
XmlSerializer(typeof(FileVariablesCollection));
TextReader reader = new StreamReader(filename);
FileVariablesCollection =
(FileVariablesCollection)ser.Deserialize(reader);
reader.Close();
return FileVariablesCollection;
}
It is definitely reading the file (filename) with the XML defined
above
The definition for the CollectionBase is as follows:
namespace TestStuff
{
/// <summary>
/// </summary>
[Serializable]
public class FileVariables
{
public string UserID;
public string FileName;
public string FileType;
public string TemplateFolder;
public string ScriptFolder;
public string ResultsFolder;
public string SavedScriptPrefix;
public string SRPRGCARNo;
public string ResultsFileType;
public string SavedResultsSuffix;
}
[Serializable]
public class FileVariablesCollection:CollectionBase
{
public virtual void Add(
string UserID,
string FileName,
string FileType,
string TemplateFolder,
string ScriptFolder,
string ResultsFolder,
string SavedScriptPrefix,
string SRPRGCARNo,
string ResultsFileType,
string SavedResultsSuffix)
{
FileVariables ds = new FileVariables();
ds.UserID = UserID;
ds.FileName = FileName;
ds.FileType = FileType;
ds.TemplateFolder = TemplateFolder;
ds.ScriptFolder = ScriptFolder;
ds.ResultsFolder = ResultsFolder;
ds.SavedScriptPrefix = SavedScriptPrefix;
ds.SRPRGCARNo = SRPRGCARNo;
ds.ResultsFileType = ResultsFileType;
ds.SavedResultsSuffix = SavedResultsSuffix;
this.Add(ds);
}
public virtual void Add(FileVariables NewFileVariables)
{
this.List.Add(NewFileVariables);
FileVariablesFactory.Save(this);
}
public virtual FileVariables this[int Index]{get{return
(FileVariables)this.List[Index];}}
public class FileVariablesFactory
{
public const string configFile = "UserDetails.config";
public static FileVariablesCollection GetFileVariables()
{
FileVariablesCollection FileVariablesCollection;
string filename =
Path.Combine(Path.GetDirectoryName(Directory.GetCurrentDirectory()),
configFile);
if(!System.IO.File.Exists(filename))
return (new FileVariablesCollection());
XmlSerializer ser = new
XmlSerializer(typeof(FileVariablesCollection));
TextReader reader = new StreamReader(filename);
FileVariablesCollection =
(FileVariablesCollection)ser.Deserialize(reader);
reader.Close();
return FileVariablesCollection;
}
public static void Save(FileVariablesCollection
FileVariablesCollection)
{
string filename =
Path.Combine(Path.GetDirectoryName(Directory.GetCurrentDirectory()),
configFile);
XmlSerializer ser = new
XmlSerializer(typeof(FileVariablesCollection));
TextWriter writer = new StreamWriter(filename);
ser.Serialize(writer, FileVariablesCollection);
writer.Close();
}
}
}
Thus, you have the code and the xml. Any ideas why this
FileVariablesCollection =
(FileVariablesCollection)ser.Deserialize(reader) statement is failing?
The xml and variable names seem to match and i am stumped! This was
working until just recently but I am unsure as to how i could have
latered this code or the xml file to cause this issue (the xml file
has only been updated via the collectionbase)
failing with the following message:
An unhandled exception of type 'System.InvalidOperationException'
occurred in system.xml.dll
Additional information: There is an error in XML document (15, 3).
The statement it is failing on is below:
FileVariablesCollection =
(FileVariablesCollection)ser.Deserialize(reader);
The xml document being read is below:
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfFileVariables xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<FileVariables>
<UserID>mark.r.wingfield</UserID>
<FileName>Test Script name 4.sql</FileName>
<FileType>Template</FileType>
<TemplateFolder>c:\template</TemplateFolder>
<ScriptFolder>c:\script</ScriptFolder>
<ResultsFolder>c:\results</ResultsFolder>
<SavedScriptPrefix>PREFIXY stuff</SavedScriptPrefix>
<SRPRGCARNo>SR222222A</SRPRGCARNo>
<ResultsFileType>Template</ResultsFileType>
<SavedResultsSuffix>.csv</SavedResultsSuffix>
</FileVariables>
</ArrayOfFileVariables>
The full code for the failing method is:
public static FileVariablesCollection GetFileVariables()
{
FileVariablesCollection FileVariablesCollection;
string filename =
Path.Combine(Path.GetDirectoryName(Directory.GetCurrentDirectory()),
configFile);
if(!System.IO.File.Exists(filename))
return (new FileVariablesCollection());
XmlSerializer ser = new
XmlSerializer(typeof(FileVariablesCollection));
TextReader reader = new StreamReader(filename);
FileVariablesCollection =
(FileVariablesCollection)ser.Deserialize(reader);
reader.Close();
return FileVariablesCollection;
}
It is definitely reading the file (filename) with the XML defined
above
The definition for the CollectionBase is as follows:
namespace TestStuff
{
/// <summary>
/// </summary>
[Serializable]
public class FileVariables
{
public string UserID;
public string FileName;
public string FileType;
public string TemplateFolder;
public string ScriptFolder;
public string ResultsFolder;
public string SavedScriptPrefix;
public string SRPRGCARNo;
public string ResultsFileType;
public string SavedResultsSuffix;
}
[Serializable]
public class FileVariablesCollection:CollectionBase
{
public virtual void Add(
string UserID,
string FileName,
string FileType,
string TemplateFolder,
string ScriptFolder,
string ResultsFolder,
string SavedScriptPrefix,
string SRPRGCARNo,
string ResultsFileType,
string SavedResultsSuffix)
{
FileVariables ds = new FileVariables();
ds.UserID = UserID;
ds.FileName = FileName;
ds.FileType = FileType;
ds.TemplateFolder = TemplateFolder;
ds.ScriptFolder = ScriptFolder;
ds.ResultsFolder = ResultsFolder;
ds.SavedScriptPrefix = SavedScriptPrefix;
ds.SRPRGCARNo = SRPRGCARNo;
ds.ResultsFileType = ResultsFileType;
ds.SavedResultsSuffix = SavedResultsSuffix;
this.Add(ds);
}
public virtual void Add(FileVariables NewFileVariables)
{
this.List.Add(NewFileVariables);
FileVariablesFactory.Save(this);
}
public virtual FileVariables this[int Index]{get{return
(FileVariables)this.List[Index];}}
public class FileVariablesFactory
{
public const string configFile = "UserDetails.config";
public static FileVariablesCollection GetFileVariables()
{
FileVariablesCollection FileVariablesCollection;
string filename =
Path.Combine(Path.GetDirectoryName(Directory.GetCurrentDirectory()),
configFile);
if(!System.IO.File.Exists(filename))
return (new FileVariablesCollection());
XmlSerializer ser = new
XmlSerializer(typeof(FileVariablesCollection));
TextReader reader = new StreamReader(filename);
FileVariablesCollection =
(FileVariablesCollection)ser.Deserialize(reader);
reader.Close();
return FileVariablesCollection;
}
public static void Save(FileVariablesCollection
FileVariablesCollection)
{
string filename =
Path.Combine(Path.GetDirectoryName(Directory.GetCurrentDirectory()),
configFile);
XmlSerializer ser = new
XmlSerializer(typeof(FileVariablesCollection));
TextWriter writer = new StreamWriter(filename);
ser.Serialize(writer, FileVariablesCollection);
writer.Close();
}
}
}
Thus, you have the code and the xml. Any ideas why this
FileVariablesCollection =
(FileVariablesCollection)ser.Deserialize(reader) statement is failing?
The xml and variable names seem to match and i am stumped! This was
working until just recently but I am unsure as to how i could have
latered this code or the xml file to cause this issue (the xml file
has only been updated via the collectionbase)