XmlSerializer.Deserialize, Method not found

I am deserializing from an XML file to a C# .NET 1.1 class in a plugin for ACT!2006. On my development machine (of course) the deserialization works fine. On the end-user machine an exception is thrown during XmlSerializer.Deserialize saying that a setter method is not found. If I remove one property field, the next will throw an exception. Here's an example of the exception text:

There is an error in the XML document.
Method not found: Void MyAssembly.MyClass.set_MyField(System.String).
Stack Trace:    at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
   at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, XmlDeserializationEvents events)
   at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader)
   at MyNamespace.MyXmlSerializer.Read(String fn, Type type) in c:\...\myxmlserializer.cs:line 37
   at ...


Both computers are Windows XP  running .NET 1.1 SP1. My Dev is SP2, the Target is SP3. Both are using System.XML Version 1.0.5000.0.

I initially had some issues with UTF-8 vs UTF-16 encoding of these files. I believe I solved that by saving the files with a proper encoding from Visual Studio.

If both machines are using the same version of the framework, why would one fail to find the method and the other find it just fine? Maybe it's one of those screwy Xml Serialization errors and has something to do with permissions on the Temp directory?

I've been banging my head against this for about 8 hours now.

Thanks Anybody!
/cm

// the serialization routines
 
public static void Write(string fn, object o)
{
	XmlSerializer ser = new XmlSerializer( o.GetType());
 
	System.Text.StringBuilder sb = new System.Text.StringBuilder();
	System.IO.StringWriter sw = new StringWriter(sb);
	ser.Serialize( sw, o);
	XmlDocument doc = new XmlDocument();
	doc.LoadXml( sb.ToString() );
	doc.Save(fn);																			  
}
 
public static object Read(string fn, Type type)
{
	XmlDocument doc = new XmlDocument();
	doc.Load(fn);
 
	//Assuming doc is an XML document containing a serialized object and objType is a System.Type set to the type of the object.
	XmlNodeReader reader = new XmlNodeReader(doc.DocumentElement);
	XmlSerializer ser = new XmlSerializer(type);
 
	object obj = ser.Deserialize(reader);
	// Then you just need to cast obj into whatever type it is eg:
	return obj;
}
 
// the class to be deserialized (in VB.NET)
Public Class MyClass
    Private _MyField As String
    Public Property MyField() As String
        Get
            Return _MyField
        End Get
        Set(ByVal Value As String)
            _MyField = Value
        End Set
    End Property
 
    ' ... other fields and methods
End Class

Open in new window

cod3monk3yincAsked:
Who is Participating?
 
abelConnect With a Mentor Commented:
There are some limitations with the XmlSerializer, which are described here: http://www.codeproject.com/KB/XML/xmlserializerforunknown.aspx. It also discusses your error.

What I miss in your code is the Serializable attribute in the class that needs to be serialized. Without it, only the base classes will be serialized. Use your code like this:

// the class to be deserialized (in VB.NET)
<Serializable()> _
Public Class MyClass
    Private _MyField As String
    Public Property MyField() As String
In addition, you may have run into a bug that has been reported by Microsoft and affects all .NET 1.1 installations. The report of the bug is here: http://support.microsoft.com/kb/814187, including a workaround.
I understand that this is a very old question. However, it is quite likely you still have this issue or that people running into the same issue require a solution. Which is why I posted.

-- Abel --
0
 
cod3monk3yincAuthor Commented:
To clarify, dev machine is XP SP2 with .NET 1.0, 1.1, and 2.0 installed, target machine is XP SP3 with only .NET 1.1 installed. The XML file to be deserialized is shown in the snipped below.

I've coded the XML file by hand. If I create a new object, then Serialize it to disk and then immediately Deserialize it to another object, everything works fine (on both machines). It seems to be this hand-crafted XML file that I'm having problems with.


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

Open in new window

0
 
cod3monk3yincAuthor Commented:
Ok, this is interesting.

When I made a TEST APPLICATION that did serialize/deserialize everything worked fine.

When I run from ACT!2006, I am unable to serialize. This is different from what I spoke(wrote) earlier. Forgive me, I'm tired! I'm attaching the dump from my attempt to Serialize a new instance of my class during the OnLoad routine of the plugin.

The target machine is running without a password. I right clicked the shortcut that starts the application, and went to Shortcut > Advanced and checked [x] Run with different credentials. After clicking the shortcut, I unchecked [ ] Protect my computer and data from unauthorized program activity. This changed nothing. The user is an Administrator on the computer.

Starting to get very lost here...

/cm
OnLoad Exception: 
 
System.InvalidOperationException: There was an error generating the XML document. ---> System.InvalidCastException: Specified cast is not valid.
   at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write3_MyClass(Object o)
   --- End of inner exception stack trace ---
   at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle)
   at System.Xml.Serialization.XmlSerializer.Serialize(TextWriter textWriter, Object o)
   at MyXmlSerializer.Write(String fn, Object o) in c:\...\myxmlserializer.cs:line 20
   at ACTPlugIn.Class1.OnLoad(ActApplication App) in C:\..\ActPlugin\Class1.vb:line 263

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.