XML Serialization of XElement property

352 views
Skip to first unread message

James Bradt

unread,
Oct 21, 2009, 1:06:19 PM10/21/09
to masstransit-discuss
I came across an issue today with regards to the custom xml
serialization.

If the property is of type XElement, the system throws an exception on
deserialization

Any ideas that can point me in the right direction?

Thanks,
James

======================

Unrecognized message msmq://machinename/queuename:0d3aed87-adcd-44e6-8dac-690f91be34b8\50072
System.Runtime.Serialization.SerializationException: Failed to
serialize message
---> System.NullReferenceException: Object reference not set to an
instance of an object.
at Magnum.Reflection.TypeInstanceFactory`1.New() in d:\Home\Magnum
\trunk\src\Magnum\Reflection\TypeInstanceFactory.cs:line 45
at Magnum.Reflection.ClassFactory.New(Type type) in d:\Home\Magnum
\trunk\src\Magnum\Reflection\ClassFactory.cs:line 38
at MassTransit.Serialization.Custom.ObjectDeserializer`1.Deserialize
(IDeserializerContext context) in d:\Projects\Libraries\masstransit\src
\MassTransit\Serialization\Custom\ObjectDeserializer.cs:line 33
at MassTransit.Serialization.Custom.DeserializerContext.Deserialize
(String ns) in d:\Projects\Libraries\masstransit\src\MassTransit
\Serialization\Custom\DeserializerContext.cs:line 95
at
MassTransit.Serialization.Custom.ObjectDeserializer`1.ReadProperty
(IDeserializerContext context, T instance) in d:\Projects\Libraries
\masstransit\src\MassTransit\Serialization\Custom
\ObjectDeserializer.cs:line 66
at MassTransit.Serialization.Custom.ObjectDeserializer`1.Deserialize
(IDeserializerContext context) in d:\Projects\Libraries\masstransit\src
\MassTransit\Serialization\Custom\ObjectDeserializer.cs:line 48
at MassTransit.Serialization.Custom.DeserializerContext.Deserialize
(String ns) in d:\Projects\Libraries\masstransit\src\MassTransit
\Serialization\Custom\DeserializerContext.cs:line 95
at
MassTransit.Serialization.Custom.ObjectDeserializer`1.ReadProperty
(IDeserializerContext context, T instance) in d:\Projects\Libraries
\masstransit\src\MassTransit\Serialization\Custom
\ObjectDeserializer.cs:line 66
at MassTransit.Serialization.Custom.ObjectDeserializer`1.Deserialize
(IDeserializerContext context) in d:\Projects\Libraries\masstransit\src
\MassTransit\Serialization\Custom\ObjectDeserializer.cs:line 48
at MassTransit.Serialization.Custom.DeserializerContext.Deserialize
(String ns) in d:\Projects\Libraries\masstransit\src\MassTransit
\Serialization\Custom\DeserializerContext.cs:line 95
at
MassTransit.Serialization.Custom.ObjectDeserializer`1.ReadProperty
(IDeserializerContext context, T instance) in d:\Projects\Libraries
\masstransit\src\MassTransit\Serialization\Custom
\ObjectDeserializer.cs:line 66
at MassTransit.Serialization.Custom.ObjectDeserializer`1.Deserialize
(IDeserializerContext context) in d:\Projects\Libraries\masstransit\src
\MassTransit\Serialization\Custom\ObjectDeserializer.cs:line 48
at MassTransit.Serialization.Custom.DeserializerContext.Deserialize
(String ns) in d:\Projects\Libraries\masstransit\src\MassTransit
\Serialization\Custom\DeserializerContext.cs:line 95
at MassTransit.Serialization.Custom.DeserializerContext.Deserialize
() in d:\Projects\Libraries\masstransit\src\MassTransit\Serialization
\Custom\DeserializerContext.cs:line 77
at
MassTransit.Serialization.Custom.CustomXmlSerializer.DeserializeMessage
(XmlReader reader) in d:\Projects\Libraries\masstransit\src\MassTransit
\Serialization\Custom\CustomXmlSerializer.cs:line 126
at MassTransit.Serialization.Custom.CustomXmlSerializer.Deserialize
(Stream input) in d:\Projects\Libraries\masstransit\src\MassTransit
\Serialization\Custom\CustomXmlSerializer.cs:line 93
at MassTransit.Serialization.XmlMessageSerializer.Deserialize
(Stream stream) in d:\Projects\Libraries\masstransit\src\MassTransit
\Serialization\XmlMessageSerializer.cs:line 57
--- End of inner exception stack trace ---
at MassTransit.Serialization.XmlMessageSerializer.Deserialize
(Stream stream) in d:\Projects\Libraries\masstransit\src\MassTransit
\Serialization\XmlMessageSerializer.cs:line 81
at
MassTransit.Transports.Msmq.MsmqEndpoint.<>c__DisplayClass5.<ReceiveFromTransport>b__3
(Message message) in d:\Projects\Libraries\masstransit\src\Transports
\MassTransit.Transports.Msmq\MsmqEndpoint.cs:line 103

=========================================

Chris Deweese

unread,
Oct 21, 2009, 2:19:52 PM10/21/09
to masstrans...@googlegroups.com
If it is a System.Xml.Linq.XElement then you cannot serialize those using
the .NET Xml Serialization. Same goes for XDocument. We ran into this
quite a bit on some big Xml projects.

Chris

---
Christopher Deweese
Microsoft MVP, Solutions Architect
http://christopherDeweese.com/
Twitter: @cdeweese

"Architecture is a 12-letter word for planning." -Kshemendra Paul, Federal
Chief Architect

Dru Sellers

unread,
Oct 21, 2009, 2:28:21 PM10/21/09
to masstrans...@googlegroups.com
It's probably the MT XML serializer.

Chris P?

-d

On Oct 21, 2009, at 1:19 PM, "Chris Deweese" <chris....@gmail.com>
wrote:

James Bradt

unread,
Oct 21, 2009, 3:23:58 PM10/21/09
to masstransit-discuss
This is using the MT serializer...

James

On Oct 21, 2:19 pm, "Chris Deweese" <chris.dewe...@gmail.com> wrote:
> If it is a System.Xml.Linq.XElement then you cannot serialize those using
> the .NET Xml Serialization.  Same goes for XDocument.  We ran into this
> quite a bit on some big Xml projects.
>
> Chris
>
> ---
> Christopher Deweese
> Microsoft MVP, Solutions Architecthttp://christopherDeweese.com/

Chris Deweese

unread,
Oct 21, 2009, 3:57:10 PM10/21/09
to masstrans...@googlegroups.com
Yeah, my bad. I need to learn to read :)

Chris

---
Christopher Deweese
Microsoft MVP, Solutions Architect
http://christopherDeweese.com/
Twitter: @cdeweese

"Architecture is a 12-letter word for planning." -Kshemendra Paul, Federal
Chief Architect


-----Original Message-----
From: masstrans...@googlegroups.com
[mailto:masstrans...@googlegroups.com] On Behalf Of James Bradt
Sent: Wednesday, October 21, 2009 2:24 PM
To: masstransit-discuss

Chris Patterson

unread,
Oct 21, 2009, 7:12:02 PM10/21/09
to masstrans...@googlegroups.com
I guess I would ask, what are you trying to serialize an XElement?

If you have some XML that you want to include as part of your message, it would make sense to make that a string within the class.

Then you could parse that XML on the receiving end into whatever type of XML parser you wanted to use (XElement, Dom, whatever).

An "XElement" just doesn't seem like something you would want to include in a message directly.



Chris Patterson



Chris Deweese

unread,
Oct 22, 2009, 10:17:12 AM10/22/09
to masstrans...@googlegroups.com

Just as a comment, because I have been through this – I would look at another way to send the content you are sending.  We dealt a lot with very large (emphasis on very) Xml models at my last job and honestly there were times when we sent Xml content as a string and the receiver just picked it up and used XDocument or XElement.Parse to get the valid Xml.

 

Serializing an XDocument or XElement wouldn’t make much sense because it is already Xml content.

 

We took the string approach because the content we worked with was pushing the performance limits of serialization.  The Linq.Xml stuff is just plain faster, but the trade-off is that you can’t serialize it out of the box.

 

 

---

Christopher Deweese

Microsoft MVP, Solutions Architect

http://christopherDeweese.com/

Twitter: @cdeweese

 

"Architecture is a 12-letter word for planning." -Kshemendra Paul, Federal Chief Architect

 

From: masstrans...@googlegroups.com [mailto:masstrans...@googlegroups.com] On Behalf Of Chris Patterson
Sent: Wednesday, October 21, 2009 6:12 PM
To: masstrans...@googlegroups.com
Subject: [masstransit-discuss] Re: XML Serialization of XElement property

 

I guess I would ask, what are you trying to serialize an XElement?

James Bradt

unread,
Oct 22, 2009, 10:21:32 AM10/22/09
to masstransit-discuss
Chris,

The xelement in this case is engineering information about an parts
for an order that could drastically vary in size and structure.

My thinking was to include a property called EngineeringData that
contains this information so that it could easily be transported
within the message. Seems to me that creating a specific object
structure would be a never ending task.

As you suggested above, I have already changed the property to be a
string which is parsed into/outof the xelement as it is used by the
serializer/deserializer.

Didn't know if there was something that would make it work without
this small work-around.

Thanks,
James.
> chrisfromtu...@gmail.com

Chris Deweese

unread,
Oct 22, 2009, 10:33:52 AM10/22/09
to masstrans...@googlegroups.com
Yeah that is a hard situation for sure. In traditional serialization you
could make the class property type object and then use the choice structure
to choose the actual type, but that makes your code a little more complex
because you have to type check on the receiving end.

I typically prefer to be explicit in all cases but sometimes simply having a
string that you expect to be valid Xml is just plain simpler.

We tried a variety of approaches but settled on the string as it was just
simpler and then we used LINQ to Xml to parse the XElement to a class on the
other side.

I posted an example of this at CodePlex: http://linq2niem.codeplex.com/ We
basically used a class that took an XDocument in the constructor and then
the property getters use Linq to Xml queries to get the data out of the
document. It worked well for us, but we had a lot of common base elements
and then some variation on things that may or may not be included.

If you have any questions feel free to email me directly. This really isn't
an MT issue :)

</endHijack>

Chris

---
Christopher Deweese
Microsoft MVP, Solutions Architect
http://christopherDeweese.com/
Twitter: @cdeweese

"Architecture is a 12-letter word for planning." -Kshemendra Paul, Federal
Chief Architect


-----Original Message-----
From: masstrans...@googlegroups.com
[mailto:masstrans...@googlegroups.com] On Behalf Of James Bradt
Sent: Thursday, October 22, 2009 9:22 AM
To: masstransit-discuss
Subject: [masstransit-discuss] Re: XML Serialization of XElement property


Reply all
Reply to author
Forward
0 new messages