XML and collectionbase

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)
 
S

stainless

Well what's in line 15 column 3 in the XML. It could be an invalid
character that can't be interpreted.

That is the "A" in </ArrayOfFileVariables>

Looks valid to me
 
A

Arne Vajhøj

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

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)

Very difficult to say because the posted code and XML works fine.

Arne
 
S

stainless

I altered the xml to be as below:

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfFileVariables xmlns="http://tempuri.org/UserDetails.xsd"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://
www.w3.org/2001/XMLSchema-instance">
</ArrayOfFileVariables>

Thus, no data within ArrayOfVariables and tried to add data using my
code. This uses the same GetFileVariables() posted above and this
failed at the same point, this time due to column 2, row 2.

Then tried with the following xml:

<ArrayOfFileVariables>
</ArrayOfFileVariables>

This worked and created the following xml after the insertion:

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

Then ran my process to update this row. Failed again on the same line
of code in GetVariables, this time indicating 15,3.

This is very strange. I even changed all the innsertext data manually
to be "xxxxx", just in case there was an issue with the physical data.
Still failed.

Another attempt in desparation was to simply reduce the number of
variables in the xml to just 1 (i.e. UserId). GetVariables failed at
6,3. Xml in this case looked like:

<?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>xxxxx</UserID>
</FileVariables>
</ArrayOfFileVariables>


Running out of ideas now as I am new to xml and this is becoming too
much of a struggle (tight timescales and all that).

Any simple tried and tested xml solutions for the data structures
above (using .xsd maybe, although I have never used these files)?

Basically I need to be able to:

1. Add a new FileVariable record structure at a specified position in
the xml for a userId (e.g. index 0, 1 for userid Joe Bloggs etc)
2. Delete a specified FileVariable record using the UserId value and
the index for that user (e.g. remove the 2nd sequential entry for
userid Joe Bloggs)
3. Update a specified FileVariable record using the UserId value and
the index for that user
4. Delete all FileVariable records for that Userid

Desperate for any help. Oh, and by the way, stuck with .Net Framework
1.1, so any whizzy processes to crate code automatically may not be
available to me. Sorry, the contract i am on is a little in the dark
ages.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top