Welcome to Windows Communication Foundation (WCF)
Top Tasks :

WCF Team Bloggers

Reading Messages for Validation

How do I perform XML validation against an entire message? There is a method to read the body of the message but it's only possible to read headers one at a time. Although it sounds more complicated than it really is, a straightforward way to read an entire message is to write it instead. With the Message interface, you have more flexibility when writing a message to an XmlWriter than when reading from a generated XmlReader. Exchanging the roles of readers and writers is a common trick used in XML processing. Here's a really short example of using an XmlWriter with some seekable storage. I've replaced the validation step with dumping out the contents of the message. Message message = Message.CreateMessage(MessageVersion.Soap12WSAddressing10, "action!" , "body" ); MemoryStream stream = new MemoryStream(); using (message) { using (XmlDictionaryWriter writer = XmlDictionaryWriter.CreateTextWriter(stream, Encoding.UTF8, false )) { writer.WriteStartDocument(); message.WriteMessage(writer); writer.WriteEndDocument(); } } stream.Seek(0L, SeekOrigin.Begin); Console.WriteLine( new StreamReader(stream).ReadToEnd()); Obviously I'm leaving out more details here than just the validation step. For example, you'd probably want to harvest the original message for things like the message version and message properties so that you could reconstruct the message on the other side, rather than just throwing everything away. Next time: Importing and Exporting WSDL Annotations Read More...
Published Thursday, January 24, 2008 8:00 AM by Nicholas Allen's Indigo Blog
Filed under: , ,

Comments

No Comments
Anonymous comments are disabled

Copyright © 2006 Microsoft Corporation. All Rights Reserved. | Terms of Use | Privacy Statement | Contact Us