You may have a scenario where an XML representation of a BusinessEntity class needs to be parsed into an in-memory object. One example is parsing the XML representation of DynamicEntity into an instance of this class within a plug-in assembly.
This sample is a stand-alone application that demonstrates how this can be done. Included here are two sample XML files that can be successfully parsed by this application. The sample parses any BusinessEntity XML string, and if the entity is of the type DynamicEntity, its properties are printed to the console. Child entities such as calendar rules on a calendar are handled as well.
namespace CrmClient
{
using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using DynamicEntity.CrmServer;
class App
{
public static void Main(string[] args)
{
App theApp = new App(args);
theApp.Run();
}
public App(string[] args)
{
_args = args;
}
private void Run()
{
if (_args.Length < 1)
{
Console.WriteLine("Usage: DynamicEntityTest");
return;
}
string fileName = _args[0];
Console.WriteLine("Reading XML from file {0}", fileName);
// Use stream reader to display XML to be parsed.
StreamReader sr = new StreamReader(fileName);
string entityXml = sr.ReadToEnd();
Console.WriteLine("XML to be parsed:");
Console.WriteLine(new string('-', 60));
Console.WriteLine("{0}", entityXml);
Console.WriteLine(new string('-', 60));
// Re-open the stream so that position is at the beginning of XML.
sr = new StreamReader(fileName);
// De-serialize the XML stream using the .NET XmlSerializer class.
XmlRootAttribute root = new XmlRootAttribute("BusinessEntity");
root.Namespace
= "http://schemas.microsoft.com/crm/2006/WebServices";
XmlSerializer xmlSerializer
= new XmlSerializer(typeof(BusinessEntity), root);
BusinessEntity entity
= (BusinessEntity)xmlSerializer.Deserialize(sr);
// End of deserialization.
Console.WriteLine("Deserialized XML into object of type {0}",
entity.GetType().FullName);
DynamicEntity dynamicEntity = entity as DynamicEntity;
if (dynamicEntity != null)
{
DisplayDynamicEntity(dynamicEntity,
"de-serialized from XML string");
}
}
///
/// Displays DynamicEntity instance and its properties.
///
private void DisplayDynamicEntity(DynamicEntity entity,
string message)
{
const string LineFormat = " {0,-30}{1,-30}{2}";
Console.WriteLine("DynamicEntity {0}: {1} with {2} properties:",
message, entity.Name, entity.Properties.Length);
Console.WriteLine(LineFormat, "Property Name", "Property Type",
"Property Value");
Console.WriteLine(LineFormat, "---", "---", "---");
foreach (Property prop in entity.Properties)
{
Type propertyType = prop.GetType();
// Format property value based on property type.
string propertyValue = string.Empty;
if (propertyType == typeof(StringProperty))
{
propertyValue = string.Format("'{0}'",
((StringProperty)prop).Value);
}
else if (propertyType == typeof(CrmDateTimeProperty))
{
CrmDateTime dt = ((CrmDateTimeProperty)prop).Value;
propertyValue
= string.Format("'{0}' date='{1}' time='{2}'",
dt.Value, dt.date, dt.time);
}
else if (propertyType == typeof(KeyProperty))
{
Key key = ((KeyProperty)prop).Value;
propertyValue = string.Format("'{0:D}'", key.Value);
}
else if (propertyType == typeof(LookupProperty))
{
Lookup lookup = ((LookupProperty)prop).Value;
propertyValue
= string.Format("'{0:D}' name='{1}' dsc='{2}'",
lookup.Value, lookup.name, lookup.dsc);
}
else if (propertyType == typeof(StateProperty))
{
string state = ((StateProperty)prop).Value;
propertyValue = string.Format("'{0}'", state);
}
else if (propertyType == typeof(DynamicEntityArrayProperty))
{
DynamicEntity[] children
= ((DynamicEntityArrayProperty)prop).Value;
propertyValue
= string.Format("number of child entities: {0}",
children.Length);
Console.WriteLine(LineFormat, prop.Name, propertyType.Name,
propertyValue);
Console.WriteLine(new string('>', 30));
int nc = 1;
foreach (DynamicEntity child in children)
{
DisplayDynamicEntity(child,
message + " - child #" + nc.ToString());
nc++;
}
Console.WriteLine(new string('<', 30));
continue;
}
else if (propertyType == typeof(OwnerProperty))
{
Owner owner = ((OwnerProperty)prop).Value;
propertyValue
= string.Format("'{0}' name='{1}' type='{2}'",
owner.Value, owner.name, owner.type);
}
else if (propertyType == typeof(CrmBooleanProperty))
{
CrmBoolean boolean = ((CrmBooleanProperty)prop).Value;
propertyValue = string.Format("'{0}'", boolean.Value);
}
else if (propertyType == typeof(PicklistProperty))
{
Picklist picklist = ((PicklistProperty)prop).Value;
propertyValue
= string.Format("'{0}', name='{1}'", picklist.Value,
picklist.name);
}
Console.WriteLine(LineFormat, prop.Name, propertyType.Name,
propertyValue);
}
}
string[] _args;
}
}
No comments:
Post a Comment