XMLSerilaizer Question

  • Thread starter Thread starter Doug Handler
  • Start date Start date
D

Doug Handler

Hi,

I'm trying to serialize a customer exception via the XMLSerializer. I've
done it via Soap and Binary and all works fine, but ideally need it done via
the XMLSerializer. When i run the code i get an InvalidOperationException
on the first line. Any help would be extremely appreciated. Thanks in
advance. Doug

Here's the code:
public static void SerializeExceptionXML(Exception e)

{

XmlSerializer xmlFormat = new XmlSerializer(typeof(InvalidChannelException),
new Type[]{typeof(InvalidChannelException)});

stream = new FileStream(CHANNEL_EXCEPTION_FILENAME + ".xml",
FileMode.Create, FileAccess.Write, FileShare.None);

xmlFormat.Serialize(stream, e);

stream.Close();

}
 
The XmlSerializer class throws exceptions that are quite cryptic. You can
find out more about the exception by examining the InnerException property:

XmlSerializer xmlFormat=null;

try{

xmlFormat= new XmlSerializer(typeof(InvalidChannelException),
new Type[]{typeof(InvalidChannelException),typeof(System.Exception)});
}
catch(Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.InnerException.Message+ex.InnerException.StackTrace );
}


Usually this is something about a portion of the base Exception class not
being serializable and some Security or other attributes needing to be added.

Hope that helps,
Peter
 
When asking for help with an error, it is helpful when you include the
exception type and the message, if not the entire error dump.

In this case, reading the message should tell you what you need to know:
"Cannot serialize member System.Exception.Data of type
System.Collections.IDictionary, because it implements IDictionary."

Apparently, Exceptions cannot be serialized with the XmlSerializer.
 
Peter,

Thank you again for the help. Joshua seem's to have a point. The error
details are below:

A first chance exception of type 'System.InvalidOperationException' occurred
in System.Xml.dll

Cannot serialize member System.Exception.Data of type
System.Collections.IDictionary, because it implements IDictionary. at
System.Xml.Serialization.TypeScope.GetDefaultIndexer(Type type, String
memberInfo)



Peter Bromberg said:
The XmlSerializer class throws exceptions that are quite cryptic. You can
find out more about the exception by examining the InnerException
property:

XmlSerializer xmlFormat=null;

try{

xmlFormat= new XmlSerializer(typeof(InvalidChannelException),
new Type[]{typeof(InvalidChannelException),typeof(System.Exception)});
}
catch(Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.InnerException.Message+ex.InnerException.StackTrace
);
}


Usually this is something about a portion of the base Exception class not
being serializable and some Security or other attributes needing to be
added.

Hope that helps,
Peter
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com




Doug Handler said:
Hi,

I'm trying to serialize a customer exception via the XMLSerializer. I've
done it via Soap and Binary and all works fine, but ideally need it done
via
the XMLSerializer. When i run the code i get an
InvalidOperationException
on the first line. Any help would be extremely appreciated. Thanks in
advance. Doug

Here's the code:
public static void SerializeExceptionXML(Exception e)

{

XmlSerializer xmlFormat = new
XmlSerializer(typeof(InvalidChannelException),
new Type[]{typeof(InvalidChannelException)});

stream = new FileStream(CHANNEL_EXCEPTION_FILENAME + ".xml",
FileMode.Create, FileAccess.Write, FileShare.None);

xmlFormat.Serialize(stream, e);

stream.Close();

}
 
Joshua,

Sorry for lack of details...it just seemed to me at the time it was weird
that I can do it either via Binary or SOAP but not XML. I'd have to
implement idictonary, iterate through, do a lot of custom work when Binary
will work fine.

dh
Joshua Flanagan said:
When asking for help with an error, it is helpful when you include the
exception type and the message, if not the entire error dump.

In this case, reading the message should tell you what you need to know:
"Cannot serialize member System.Exception.Data of type
System.Collections.IDictionary, because it implements IDictionary."

Apparently, Exceptions cannot be serialized with the XmlSerializer.



Doug said:
Hi,

I'm trying to serialize a customer exception via the XMLSerializer. I've
done it via Soap and Binary and all works fine, but ideally need it done
via the XMLSerializer. When i run the code i get an
InvalidOperationException on the first line. Any help would be extremely
appreciated. Thanks in advance. Doug

Here's the code:
public static void SerializeExceptionXML(Exception e)

{

XmlSerializer xmlFormat = new
XmlSerializer(typeof(InvalidChannelException), new
Type[]{typeof(InvalidChannelException)});

stream = new FileStream(CHANNEL_EXCEPTION_FILENAME + ".xml",
FileMode.Create, FileAccess.Write, FileShare.None);

xmlFormat.Serialize(stream, e);

stream.Close();

}
 
It can be confusing. The binary and soap formatters work differently
than the XML serializer (notice they are in 2 different namespace
hierarchies). The XML serializer serializes only publicly exposed
properties, while the binary/soap methods serialze the entire internal
state of an object (therefore requiring more security demands).


Doug said:
Joshua,

Sorry for lack of details...it just seemed to me at the time it was weird
that I can do it either via Binary or SOAP but not XML. I'd have to
implement idictonary, iterate through, do a lot of custom work when Binary
will work fine.

dh
When asking for help with an error, it is helpful when you include the
exception type and the message, if not the entire error dump.

In this case, reading the message should tell you what you need to know:
"Cannot serialize member System.Exception.Data of type
System.Collections.IDictionary, because it implements IDictionary."

Apparently, Exceptions cannot be serialized with the XmlSerializer.



Doug said:
Hi,

I'm trying to serialize a customer exception via the XMLSerializer. I've
done it via Soap and Binary and all works fine, but ideally need it done
via the XMLSerializer. When i run the code i get an
InvalidOperationException on the first line. Any help would be extremely
appreciated. Thanks in advance. Doug

Here's the code:
public static void SerializeExceptionXML(Exception e)

{

XmlSerializer xmlFormat = new
XmlSerializer(typeof(InvalidChannelException), new
Type[]{typeof(InvalidChannelException)});

stream = new FileStream(CHANNEL_EXCEPTION_FILENAME + ".xml",
FileMode.Create, FileAccess.Write, FileShare.None);

xmlFormat.Serialize(stream, e);

stream.Close();

}
 
Back
Top