hi ,
The Code like this:
public static Mapping Deserialize(str ing strXmlFilePath)
{
XmlSerializer s = new XmlSerializer(t ypeof(Mapping)) ;
using (StreamReader sr = new StreamReader(st rXmlFilePath))
{
object o = s.Deserialize(s r);
Mapping m = (Mapping)o;
return m;
}
}
Sometimes ,it will throw an InvalidCastExce ption.
The strXmlFilePath refer to a same file ,but sometimes it works well
,sometimes it throws a exception.
I can't catch this exception in a debuger,because it always works well in
the debuger.
Anyone know what's wrong?
thanks 5 1321
if the Deserialize function fails it will return null and when you try
to cast that to Mapping you'll get an invalid cast exception. It's
probably working in Debug mode because your using a relative path.
Jan
Jan <ja***********@ gmail.com> wrote: if the Deserialize function fails it will return null and when you try to cast that to Mapping you'll get an invalid cast exception. It's probably working in Debug mode because your using a relative path.
No, casting null works fine:
using System;
class Test
{
static void Main()
{
object o = null;
string s = (string)o;
Console.WriteLi ne ("s is null? {0}", s==null);
}
}
--
Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Ah yes, my mistake.
It must be the Deserialize function failing to parse you xml stream
into your mapping.
Was the object serialized in the same program or from a different
program? If from a different program, did you copy the Mapping class
file to the other program and compile or did you put it in a
ClassLibrary and compile it that way?
You need to put your mapping class in a ClassLibrary if you intend to
serialize/deserialize across applications.
I'm using the Mapping class in two program , one is an add-in for VS IDE, the
other is a form application.The exception just sometimes occurs in the
add-in. Yes, the Mapping class is put in an individual class library, the two
program reference to the same dll. And I can promise these two program all
open the same and valid file.
And I don't think in XML serialization assembly version is a problem ,
assembly version just may cause problem in binary serialization. In XML
serialization, where the class resides in or even whether the class is
complete identity is not important.
"Chris Dunaway" wrote: Was the object serialized in the same program or from a different program? If from a different program, did you copy the Mapping class file to the other program and compile or did you put it in a ClassLibrary and compile it that way?
You need to put your mapping class in a ClassLibrary if you intend to serialize/deserialize across applications.
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Johan Appelgren |
last post by:
Hi,
I'm trying to serialize a class hierarchy using
System.Xml.Serialization.XmlSerializer from a class that is loaded as a Com
object. But it fails with a InvalidCastException. I don't know what I'm
doing wrong and it seems even the most simple cases fail. I tried with a
simple class that looks like this
public class Class1
{
|
by: bob scola |
last post by:
I have a csharp, VS 2003 solution for a winform application
The application uses an object called a "matter" and the class is
defined in matter.cs.
I can load matter objects into a combobox
Matter matters= v.Matters;
comboBox1.Items.AddRange(matters);
|
by: Roy Lawson |
last post by:
I have no idea what is going on here. I wrote a simple application in
VB.NET to generate a Crystal Report, and I am now trying to move it to
ASP.NET with Crstal Enterprise. I wish I could tell you how the
report works...but I can't even get past connecting to the database
:-)
I use the typical:
Dim oConnection As New SqlConnection(strCon)
Dim cmdText As String = "SELECT * FROM viewLogin WHERE " & _
|
by: DOTNET |
last post by:
Hi,
Anybody help me regarding this error:
I am assigning the values to the session variables when the button is
clicked and passing these session variables to the next page and when I am
printing these session variables they are printing. After that I am
assigning these things in hidden object and in the form submit action I am
receiving these hidden values like the following:
|
by: QA |
last post by:
I am using a Business Scorecard Accelarator in a Sharepoint Portal 2003
using SQL Server 2005
I am getting the following error:
Error,5/7/2005 10:50:14 AM,580,AUE1\Administrator,"Specified cast is
not
valid.","Microsoft.BusinessIntelligence.Scorecard.ScorecardException:
Specified cast is not valid. --->
Microsoft.BusinessIntelligence.Scorecard.ScorecardException: Specified
cast is not valid. ---> System.InvalidCastException: Specified...
| |
by: Marc |
last post by:
Hi!
I'm working with a C# client that calls a php web service. I've
created a wrapper to call the service using .NET wsdl tool (adding a
web reference).
The call to the server works fine, it is serialized correctly, and the
server returns a response (I've captured the response and it's
correct!) but when the .NET deserialize this response, it throws the
exception "System.InvalidOperationException: There is an error in XML
|
by: Gamma |
last post by:
I'm trying to inherit subclass from System.Diagnostics.Process, but
whenever I cast a "Process" object to it's subclass, I encounter an
exception "System.InvalidCastException" ("Specified cast is not
valid"). How do I fix it ?
using System.Diagnostics;
..
..
class NewProcess: Process
{
|
by: =?Utf-8?B?YXVsZGg=?= |
last post by:
i trying to collect windows services. i'm getting inconsistencies.
the collection works every time locally. but remote collection yields
different result depending on my lab environment.
1) in the same domain i start the collection on the remote machine
i can get some 19 services without a problem then it will fail with:
'wmiService.ClassPath' threw an exception of type
'System.InvalidCastException'
|
by: rsdev |
last post by:
Hi,
I have an InvalidCastException which is completely puzzling me. I have checked all the members in the stored procedure against my data provider and seems to be ok. Also in the stack trace it says ProcessRequestMain... +3742??
Here's the error;
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in...
|
by: Joe HM |
last post by:
Hello -
I was wondering that the "cleanest" way is to determine whether a
CType() will throw an InvalidCastException? I have data I receive as
an Object and I want to convert it to a String whenever possible using
CType(lObjectDummy, String) otherwise I will just ignore it.
I could put a Try/Catch As System.InvalidCastException around it but I
was wondering if that is the best solution. I once heard that
exceptions should not be...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |