Tag Cloud

CRM 2011 (161) CRM 4.0 (144) C# (116) JScript (109) Plugin (92) Registry (90) Techpedia (77) PyS60 (68) WScript (43) Plugin Message (31) Exploit (27) ShellCode (26) FAQ (22) JavaScript (21) Killer Codes (21) Hax (18) VB 6.0 (17) Commands (16) VBScript (16) Quotes (15) Turbo C++ (13) WMI (13) Security (11) 1337 (10) Tutorials (10) Asp.Net (9) Safe Boot (9) Python (8) Interview Questions (6) video (6) Ajax (5) VC++ (5) WebService (5) Workflow (5) Bat (4) Dorks (4) Sql Server (4) Aptitude (3) Picklist (3) Tweak (3) WCF (3) regex (3) Config (2) LINQ (2) PHP (2) Shell (2) Silverlight (2) TSql (2) flowchart (2) serialize (2) ASHX (1) CRM 4.0 Videos (1) Debug (1) FetchXml (1) GAC (1) General (1) Generics (1) HttpWebRequest (1) InputParameters (1) Lookup (1) Offline Plug-ins (1) OutputParameters (1) Plug-in Constructor (1) Protocol (1) RIA (1) Sharepoint (1) Walkthrough (1) Web.config (1) design patterns (1) generic (1) iframe (1) secure config (1) unsecure config (1) url (1)

Pages

Monday, April 22, 2013

Deserialize XML into an Instance of BusinessEntity

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.


   1:  //# Deserialize XML into an Instance of BusinessEntity
   2:  namespace CrmClient
   3:  {
   4:     using System;
   5:     using System.IO;
   6:     using System.Xml;
   7:     using System.Xml.Serialization;
   8:     using DynamicEntity.CrmServer;
   9:   
  10:     class App
  11:     {
  12:        public static void Main(string[] args)
  13:        {
  14:           App theApp = new App(args);
  15:   
  16:           theApp.Run();
  17:        }
  18:   
  19:        public App(string[] args)
  20:        {
  21:           _args = args;
  22:        }
  23:   
  24:        private void Run()
  25:        {
  26:           if (_args.Length < 1)
  27:           {
  28:              Console.WriteLine("Usage: DynamicEntityTest <input-file>");
  29:              return;
  30:           }
  31:   
  32:           string fileName = _args[0];
  33:           Console.WriteLine("Reading XML from file {0}", fileName);
  34:   
  35:           // Use stream reader to display XML to be parsed.
  36:           StreamReader sr = new StreamReader(fileName);
  37:           string entityXml = sr.ReadToEnd();
  38:           Console.WriteLine("XML to be parsed:");
  39:           Console.WriteLine(new string('-', 60));
  40:           Console.WriteLine("{0}", entityXml);
  41:           Console.WriteLine(new string('-', 60));
  42:   
  43:           // Re-open the stream so that position is at the beginning of XML.
  44:           sr = new StreamReader(fileName);
  45:   
  46:           // De-serialize the XML stream using the .NET XmlSerializer class.
  47:           XmlRootAttribute root = new XmlRootAttribute("BusinessEntity");
  48:           root.Namespace 
  49:                      = "http://schemas.microsoft.com/crm/2006/WebServices";
  50:           XmlSerializer xmlSerializer 
  51:                      = new XmlSerializer(typeof(BusinessEntity), root);
  52:           BusinessEntity entity 
  53:                      = (BusinessEntity)xmlSerializer.Deserialize(sr);
  54:           // End of deserialization.
  55:   
  56:           Console.WriteLine("Deserialized XML into object of type {0}",
  57:                             entity.GetType().FullName);
  58:   
  59:           DynamicEntity dynamicEntity = entity as DynamicEntity;
  60:           if (dynamicEntity != null)
  61:           {
  62:              DisplayDynamicEntity(dynamicEntity,
  63:                           "de-serialized from XML string");
  64:           }
  65:        }
  66:   
  67:        /// <summary>
  68:        /// Displays DynamicEntity instance and its properties.
  69:        /// </summary>
  70:        private void DisplayDynamicEntity(DynamicEntity entity,
  71:                                          string message)
  72:        {
  73:           const string LineFormat = "  {0,-30}{1,-30}{2}";
  74:   
  75:           Console.WriteLine("DynamicEntity {0}: {1} with {2} properties:",
  76:                         message, entity.Name, entity.Properties.Length);
  77:   
  78:           Console.WriteLine(LineFormat, "Property Name", "Property Type",
  79:                             "Property Value");
  80:           Console.WriteLine(LineFormat, "---", "---", "---");
  81:   
  82:           foreach (Property prop in entity.Properties)
  83:           {
  84:              Type propertyType = prop.GetType();
  85:   
  86:              // Format property value based on property type.
  87:              string propertyValue = string.Empty;
  88:              if (propertyType == typeof(StringProperty))
  89:              {
  90:                 propertyValue = string.Format("'{0}'",
  91:                                        ((StringProperty)prop).Value);
  92:              }
  93:              else if (propertyType == typeof(CrmDateTimeProperty))
  94:              {
  95:                 CrmDateTime dt = ((CrmDateTimeProperty)prop).Value;
  96:                 propertyValue 
  97:                        = string.Format("'{0}' date='{1}' time='{2}'",
  98:                                        dt.Value, dt.date, dt.time);
  99:              }
 100:              else if (propertyType == typeof(KeyProperty))
 101:              {
 102:                 Key key = ((KeyProperty)prop).Value;
 103:                 propertyValue = string.Format("'{0:D}'", key.Value);
 104:              }
 105:              else if (propertyType == typeof(LookupProperty))
 106:              {
 107:                 Lookup lookup = ((LookupProperty)prop).Value;
 108:                 propertyValue 
 109:                       = string.Format("'{0:D}' name='{1}' dsc='{2}'",
 110:                                  lookup.Value, lookup.name, lookup.dsc);
 111:              }
 112:              else if (propertyType == typeof(StateProperty))
 113:              {
 114:                 string state = ((StateProperty)prop).Value;
 115:                 propertyValue = string.Format("'{0}'", state);
 116:              }
 117:              else if (propertyType == typeof(DynamicEntityArrayProperty))
 118:              {
 119:                 DynamicEntity[] children 
 120:                          = ((DynamicEntityArrayProperty)prop).Value;
 121:                 propertyValue 
 122:                          = string.Format("number of child entities: {0}",
 123:                                          children.Length);
 124:   
 125:                 Console.WriteLine(LineFormat, prop.Name, propertyType.Name,
 126:                                   propertyValue);
 127:   
 128:                 Console.WriteLine(new string('>', 30));
 129:   
 130:                 int nc = 1;
 131:                 foreach (DynamicEntity child in children)
 132:                 {
 133:                    DisplayDynamicEntity(child,
 134:                               message + " - child #" + nc.ToString());
 135:                    nc++;
 136:                 }
 137:   
 138:                 Console.WriteLine(new string('<', 30));
 139:   
 140:                 continue;
 141:              }
 142:              else if (propertyType == typeof(OwnerProperty))
 143:              {
 144:                 Owner owner = ((OwnerProperty)prop).Value;
 145:                 propertyValue 
 146:                      = string.Format("'{0}' name='{1}' type='{2}'",
 147:                                owner.Value, owner.name, owner.type);
 148:              }
 149:              else if (propertyType == typeof(CrmBooleanProperty))
 150:              {
 151:                 CrmBoolean boolean = ((CrmBooleanProperty)prop).Value;
 152:                 propertyValue = string.Format("'{0}'", boolean.Value);
 153:              }
 154:              else if (propertyType == typeof(PicklistProperty))
 155:              {
 156:                 Picklist picklist = ((PicklistProperty)prop).Value;
 157:                 propertyValue 
 158:                      = string.Format("'{0}', name='{1}'", picklist.Value,
 159:                                      picklist.name);
 160:              }
 161:   
 162:              Console.WriteLine(LineFormat, prop.Name, propertyType.Name,
 163:                             propertyValue);
 164:           }
 165:        }
 166:   
 167:        string[] _args;
 168:     }
 169:  }

No comments: