This is a follow up to the previous post where I sent a strongly typed XML message from a CRM plugin to a Service Bus Queue. This is what the Xml looks like that I sent to the queue.
Note that the root node is the name of the entity. This is a snippet to show the attributes and then the formatted values. You can easily manipulate the Xml by changing the way it populates the datatable.
<contact>
<attributes>
<address1_addressid>c6fd930f-512a-42fb-a645-af4a672c740f</address1_addressid>
<address2_addressid>6a280e36-c009-463b-84f1-8666c2dfc5ba</address2_addressid>
<address2_addresstypecode>1</address2_addresstypecode>
<address2_freighttermscode>1</address2_freighttermscode>
<address2_shippingmethodcode>1</address2_shippingmethodcode>
<address3_addressid>5475a20e-3c79-479a-a1d5-05b0f144a8fc</address3_addressid>
<contactid>1d50bcf0-2519-e611-80da-5065f38b46e1</contactid>
<createdby>Charles Emes</createdby>
<createdon>13/05/2016 16:15:51</createdon>
;... more attributes
;... now the formatted values
<address2_addresstypecodename>Default Value</address2_addresstypecodename>
<address2_freighttermscodename>Default Value</address2_freighttermscodename>
<address2_shippingmethodcodename>Default Value</address2_shippingmethodcodename>
<createdonname>13/05/2016 17:15</createdonname>
</attributes></contact>
What I'm missing is a namespace and that is easy enough to add once I load it into an XmlDocument. I'm a BizTalk developer so right now I'm happy with a message that I can transform into anything I want. The attributes are in alphabetical order so I can cope with missing attributes easily enough in the XSD by making Nillable=true and MinOccurs=0. But I can also deserialize this into a contact object by creating a class form the XSD using the XSD.EXE. Note this is absolutely not a CRM Contact object but my own object. Check out my usings because there is no Microsoft.Xrm.SDK anywhere to be seen.
This receiver is just a console app and I ripped the code from another blogger. I'm just using it show what you can do with the message now you've got it.
static void Main(string[] args)
{
string connectionString = CloudConfigurationManager.GetSetting("Microsoft.ServiceBus.ConnectionString");
QueueClient Client = QueueClient.CreateFromConnectionString(connectionString, "anoqueue");
//Console.WriteLine("\nReceiving message from Queue...");
BrokeredMessage message = null;
NamespaceManager namespaceManager = NamespaceManager.Create();
while (true)
{
try
{
//receive messages from Queue
message = Client.Receive(TimeSpan.FromHours(10));
if (message != null)
{
Console.WriteLine(string.Format("Message received: Id = {0}", message.MessageId));
string s = new StreamReader(message.GetBody<Stream>(), Encoding.ASCII).ReadToEnd();
// load into an XML Document to s
System.Xml.XmlDocument xmldoc = new System.Xml.XmlDocument();
xmldoc.LoadXml(s);
// need to add a namespace because it doesn't have one
xmldoc.DocumentElement.SetAttribute("xmlns", "http://xrm.generic.schemas");
string mydocpath = @"C:\Projects\Test\ReceiveFromSB\";
xmldoc.Save(mydocpath + "saved.xml");
// Deserialize to an object too
XmlRootAttribute xRoot = new XmlRootAttribute();
xRoot.ElementName = message.Properties["EntityName"].ToString();
XmlSerializer serializer = new XmlSerializer(typeof(contact),xRoot);
StringReader rdr = new StringReader(s);
contact myContact = (contact)serializer.Deserialize(rdr);
// Now delete the message
message.Complete();
}
else
{
//no more messages in the queue
break;
}
}
catch (MessagingException me)
{
if (!me.IsTransient)
{
Console.WriteLine(me.Message);
throw;
}
else
{
// HandleTransientErrors(e);
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
throw;
}
}
Client.Close();
}}
No comments:
Post a Comment