Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Deserializing an XmlDocument using XmlMessageFormatter

504 views
Skip to first unread message

Aaron Ferrell

unread,
May 23, 2002, 7:22:12 PM5/23/02
to
I have been trying to send an XmlDocument using MSMQ. I don't want
XmlMessageFormatter to serialize it as a string e.g.
<string>&lt;...&gt;</string>. I have it so that I pass in an
XmlDocument object and it will fill the body of the message with
whatever I have in my XmlDocument, but when I go to deserialize it it
doesn't work.

// send
MessageQueue q = new MessageQueue(***);
XmlDocument doc = new XmlDocument();
// create doc and add root node and child nodes
doc.AppendChild(doc.CreateElement("Root"));
...
// this gets the entire body of the xml document
// into the message without being encoded
q.Send(doc,"XmlDocument Message");

// receive
MessageQueue q = new MessageQueue(***);
q.Formatter =
new XmlMessageFormatter(new Type[] { typeof(XmlDocument) });
Message m = q.Receive();
// this bombs out
XmlDocument doc = (XmlDocument)m.Body;

gives me "PMSystem.InvalidOperationException: Cannot deserialize the
message passed as an argument. Cannot recognize the serialization
format."

I know the message is a valid XmlDocument as I can read the BodyStream
and load it into the document using LoadXml() with no problems, but I
should be able to cast the Body property to an XmlDocument and use it
without jumping through hoops.

Any ideas?

Aaron

Frank Boyne

unread,
May 24, 2002, 4:27:01 AM5/24/02
to
"Aaron Ferrell" <aaronf...@yahoo.com> wrote in message
news:303fee88.0205...@posting.google.com...

> I have been trying to send an XmlDocument using MSMQ. I don't want
> XmlMessageFormatter to serialize it as a string

If you have an XmlDocument object and System.Messaging on both ends,
wouldn't it be simpler to just use BodyStream?

// Send


MessageQueue q = new MessageQueue(***);
XmlDocument doc = new XmlDocument();

doc.AppendChild(doc.CreateElement("Root"));
Message m = new Message ();
doc.Save (m.BodyStream)
q.Send(doc,"XmlDocument Message");

// Receive


MessageQueue q = new MessageQueue(***);

Message m = q.Receive();
XmlDocument doc = new XmlDocument ();
doc.Load (m.BodyStream);

> I know the message is a valid XmlDocument as I can read the BodyStream
> and load it into the document using LoadXml() with no problems,

The message may contain valid XML but I don't think it contains a valid
XmlMessageFormatter serialisation of an XmlDocument object which is what it
would have to be for XmlMessageFormatter to de-serialise it back into an
XmlDocument.

I think the problem is that an XmlSerializer doesn't properly handle the
XmlDocument instance. If you run doc through XmlSerializer.Serialize and
then try to run it back through Deserialize you'll find that Deserialize
fails, with an exception indicating that the root element is missing.

Off hand it isn't clear whether this is a bug in XmlSerializer or if
XmlDocument is just a class which cannot be serialized. However I suspect
the latter. XmlDocument implements IEnumerable but doesn't have an Add
method. The XmlSerializer requires that classes which implement IEnumerable
have an Add method, so XmlDocument seems to break the rules for XML
Serialization.


Aaron Ferrell

unread,
Jun 3, 2002, 11:05:18 AM6/3/02
to
Thanks,

I ended up just using the stream methods and properties, I just didn't
understand why it serialized with no errors, but then wouldn't
deserialize properly. I would think that it should give an error when
serializing if it can't deserialize it on the other end. Not much
point otherwise.

Aaron

"Frank Boyne" <frank...@unisys.com> wrote in message news:<#xvBwyvACHA.1680@tkmsftngp05>...

0 new messages