469,269 Members | 1,004 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,269 developers. It's quick & easy.

XMLSerilaizer Question

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

}
Dec 4 '05 #1
5 6126
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.Exc eption)});
}
catch(Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.InnerExcep tion.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" wrote:
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();

}

Dec 4 '05 #2
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 Handler wrote:
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();

}

Dec 4 '05 #3
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.GetDefaultIndex er(Type type, String
memberInfo)

"Peter Bromberg [C# MVP]" <pb*******@yahoo.nospammin.com> wrote in message
news:1D**********************************@microsof t.com...
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.Exc eption)});
}
catch(Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.InnerExcep tion.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" wrote:
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();

}

Dec 4 '05 #4
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" <jo**@msnews.com> wrote in message
news:O$****************@TK2MSFTNGP11.phx.gbl...
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 Handler wrote:
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();

}


Dec 4 '05 #5
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 Handler wrote:
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" <jo**@msnews.com> wrote in message
news:O$****************@TK2MSFTNGP11.phx.gbl...
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 Handler wrote:
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();

}


Dec 6 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Stevey | last post: by
10 posts views Thread by glenn | last post: by
53 posts views Thread by Jeff | last post: by
56 posts views Thread by spibou | last post: by
2 posts views Thread by Allan Ebdrup | last post: by
3 posts views Thread by Zhang Weiwu | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.