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:
Post a Comment