?
Solved

XmlSerializer.Deserialize, Method not found

Posted on 2008-11-11
4
Medium Priority
?
2,225 Views
Last Modified: 2013-11-07
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

0
Comment
Question by:cod3monk3yinc
  • 2
3 Comments
 

Author Comment

by:cod3monk3yinc
ID: 22928679
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
 

Author Comment

by:cod3monk3yinc
ID: 22928817
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
 
LVL 39

Accepted Solution

by:
abel earned 2000 total points
ID: 24232020
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question