Esempio n. 1
0
        /// <summary>
        /// Set the attribute values from SDK Webservice response on a business entity
        /// </summary>
        /// <param name="entityNode"></param>
        public void DeSerialise(XmlNode entityNode)
        {
            this.LogicalName = XmlHelper.SelectSingleNodeValue(entityNode, "LogicalName");
            this.Id          = XmlHelper.SelectSingleNodeValue(entityNode, "Id");
            XmlNode attributes     = XmlHelper.SelectSingleNode(entityNode, "Attributes");
            int     attributeCount = attributes.ChildNodes.Count;

            for (int i = 0; i < attributeCount; i++)
            {
                XmlNode node = attributes.ChildNodes[i];
                try {
                    // Add typed attribute value
                    string attributeName  = XmlHelper.SelectSingleNodeValue(node, "key");
                    object attributeValue = Attribute.DeSerialise(XmlHelper.SelectSingleNode(node, "value"), null);
                    this._attributes[attributeName] = attributeValue;

                    SetDictionaryValue(attributeName, attributeValue);
                }
                catch (Exception e) {
                    throw new Exception("Invalid Attribute Value :" + XmlHelper.GetNodeTextValue(node) + ":" + e.Message);
                }
            }
            // Get Formatted values
            XmlNode formattedValues = XmlHelper.SelectSingleNode(entityNode, "FormattedValues");

            if (formattedValues != null)
            {
                for (int i = 0; i < formattedValues.ChildNodes.Count; i++)
                {
                    XmlNode node  = formattedValues.ChildNodes[i];
                    string  key   = XmlHelper.SelectSingleNodeValue(node, "key");
                    string  value = XmlHelper.SelectSingleNodeValue(node, "value");
                    SetDictionaryValue(key + "name", value);
                    FormattedValues[key + "name"] = value;
                    object att = this._attributes[key];
                    if (att != null)
                    {
                        Script.Literal("{0}.name={1}", att, value);
                    }
                }
            }
            // Get related entities
            XmlNode relatedEntities = XmlHelper.SelectSingleNode(entityNode, "RelatedEntities");

            if (relatedEntities != null)
            {
                Dictionary <string, EntityCollection> relatedEntitiesColection = new Dictionary <string, EntityCollection>();
                for (int i = 0; i < relatedEntities.ChildNodes.Count; i++)
                {
                    XmlNode          node         = relatedEntities.ChildNodes[i];
                    XmlNode          key          = XmlHelper.SelectSingleNode(node, "key");
                    string           schemaName   = XmlHelper.SelectSingleNodeValue(key, "SchemaName");
                    Relationship     relationship = new Relationship(schemaName);
                    XmlNode          value        = XmlHelper.SelectSingleNode(node, "value");
                    EntityCollection entities     = EntityCollection.DeSerialise(value);
                    relatedEntitiesColection[relationship.SchemaName] = entities;
                }
                this.RelatedEntities = relatedEntitiesColection;
            }
        }
Esempio n. 2
0
        public static object DeSerialise(XmlNode node, string overrideType)
        {
            // Check if the value is null
            bool   isNil = (XmlHelper.GetAttributeValue(node, "i:nil") == "true");
            object value = null;

            if (!isNil)
            {
                string typeName = overrideType;
                if (typeName == null)
                {
                    typeName = _removeNsPrefix(XmlHelper.GetAttributeValue(node, "i:type"));
                }

                string stringValue = XmlHelper.GetNodeTextValue(node);
                switch (typeName)
                {
                case AttributeTypes.EntityReference:
                    EntityReference entityReferenceValue = new EntityReference(
                        new Guid(XmlHelper.SelectSingleNodeValue(node, "Id")),
                        XmlHelper.SelectSingleNodeValue(node, "LogicalName"),
                        XmlHelper.SelectSingleNodeValue(node, "Name"));

                    value = entityReferenceValue;
                    break;

                case AttributeTypes.AliasedValue:
                    value = DeSerialise(XmlHelper.SelectSingleNode(node, "Value"), null);
                    break;

                case AttributeTypes.Boolean_:
                    value = (stringValue == "true");
                    break;

                case AttributeTypes.Decimal_:
                    value = decimal.Parse(stringValue);
                    break;

                case AttributeTypes.DateTime_:
                    DateTime dateValue = DateTimeEx.Parse(stringValue);

                    // We need it in the CRM Users timezone
                    UserSettings settings = OrganizationServiceProxy.UserSettings;
                    if (settings != null)
                    {
                        // Remove the local date formating so that it is in UTC irrespective of the local timezone
                        dateValue.SetTime(dateValue.GetTime() + (dateValue.GetTimezoneOffset() * 60 * 1000));

                        DateTime localDateValue = DateTimeEx.UTCToLocalTimeFromSettings(dateValue, settings);
                        value = localDateValue;
                    }
                    else
                    {
                        value = dateValue;
                    }

                    break;

                case "guid":
                    value = new Guid(stringValue);
                    break;

                case AttributeTypes.Int_:
                    value = int.Parse(stringValue);
                    break;

                case AttributeTypes.OptionSetValue:
                    value = OptionSetValue.Parse(XmlHelper.SelectSingleNodeValue(node, "Value"));
                    break;

                case AttributeTypes.Money:
                    value = new Money(decimal.Parse(XmlHelper.SelectSingleNodeValue(node, "Value")));
                    break;

                case AttributeTypes.EntityCollection:
                    value = EntityCollection.DeSerialise(node);
                    break;

                default:
                    value = stringValue;
                    break;
                }
            }
            return(value);
        }