/// <summary>
        /// Reads the service objects collection from XML.
        /// </summary>
        /// <typeparam name="TServiceObject">The type of the service object.</typeparam>
        /// <param name="collectionXmlNamespace">Namespace of the collection XML element.</param>
        /// <param name="collectionXmlElementName">Name of the collection XML element.</param>
        /// <param name="getObjectInstanceDelegate">The get object instance delegate.</param>
        /// <param name="clearPropertyBag">if set to <c>true</c> [clear property bag].</param>
        /// <param name="requestedPropertySet">The requested property set.</param>
        /// <param name="summaryPropertiesOnly">if set to <c>true</c> [summary properties only].</param>
        /// <returns>List of service objects.</returns>
        public List <TServiceObject> ReadServiceObjectsCollectionFromXml <TServiceObject>(
            XmlNamespace collectionXmlNamespace,
            string collectionXmlElementName,
            GetObjectInstanceDelegate <TServiceObject> getObjectInstanceDelegate,
            bool clearPropertyBag,
            PropertySet requestedPropertySet,
            bool summaryPropertiesOnly) where TServiceObject : ServiceObject
        {
            List <TServiceObject> serviceObjects = new List <TServiceObject>();
            TServiceObject        serviceObject  = null;

            if (!this.IsStartElement(collectionXmlNamespace, collectionXmlElementName))
            {
                this.ReadStartElement(collectionXmlNamespace, collectionXmlElementName);
            }

            if (!this.IsEmptyElement)
            {
                do
                {
                    this.Read();

                    if (this.IsStartElement())
                    {
                        serviceObject = getObjectInstanceDelegate(this.Service, this.LocalName);

                        if (serviceObject == null)
                        {
                            this.SkipCurrentElement();
                        }
                        else
                        {
                            if (string.Compare(this.LocalName, serviceObject.GetXmlElementName(), StringComparison.Ordinal) != 0)
                            {
                                throw new ServiceLocalException(
                                          string.Format(
                                              "The type of the object in the store ({0}) does not match that of the local object ({1}).",
                                              this.LocalName,
                                              serviceObject.GetXmlElementName()));
                            }

                            serviceObject.LoadFromXml(
                                this,
                                clearPropertyBag,
                                requestedPropertySet,
                                summaryPropertiesOnly);

                            serviceObjects.Add(serviceObject);
                        }
                    }
                }while (!this.IsEndElement(collectionXmlNamespace, collectionXmlElementName));
            }

            return(serviceObjects);
        }
 /// <summary>
 /// Reads the service objects collection from XML.
 /// </summary>
 /// <typeparam name="TServiceObject">The type of the service object.</typeparam>
 /// <param name="collectionXmlElementName">Name of the collection XML element.</param>
 /// <param name="getObjectInstanceDelegate">The get object instance delegate.</param>
 /// <param name="clearPropertyBag">if set to <c>true</c> [clear property bag].</param>
 /// <param name="requestedPropertySet">The requested property set.</param>
 /// <param name="summaryPropertiesOnly">if set to <c>true</c> [summary properties only].</param>
 /// <returns>List of service objects.</returns>
 public List <TServiceObject> ReadServiceObjectsCollectionFromXml <TServiceObject>(
     string collectionXmlElementName,
     GetObjectInstanceDelegate <TServiceObject> getObjectInstanceDelegate,
     bool clearPropertyBag,
     PropertySet requestedPropertySet,
     bool summaryPropertiesOnly) where TServiceObject : ServiceObject
 {
     return(this.ReadServiceObjectsCollectionFromXml <TServiceObject>(
                XmlNamespace.Messages,
                collectionXmlElementName,
                getObjectInstanceDelegate,
                clearPropertyBag,
                requestedPropertySet,
                summaryPropertiesOnly));
 }
        /// <summary>
        /// Reads the service objects collection from JSON.
        /// </summary>
        /// <typeparam name="TServiceObject">The type of the service object.</typeparam>
        /// <param name="jsonResponse">The json response.</param>
        /// <param name="collectionJsonElementName">Name of the collection XML element.</param>
        /// <param name="getObjectInstanceDelegate">The get object instance delegate.</param>
        /// <param name="clearPropertyBag">if set to <c>true</c> [clear property bag].</param>
        /// <param name="requestedPropertySet">The requested property set.</param>
        /// <param name="summaryPropertiesOnly">if set to <c>true</c> [summary properties only].</param>
        /// <returns>List of service objects.</returns>
        internal List <TServiceObject> ReadServiceObjectsCollectionFromJson <TServiceObject>(
            JsonObject jsonResponse,
            string collectionJsonElementName,
            GetObjectInstanceDelegate <TServiceObject> getObjectInstanceDelegate,
            bool clearPropertyBag,
            PropertySet requestedPropertySet,
            bool summaryPropertiesOnly) where TServiceObject : ServiceObject
        {
            List <TServiceObject> serviceObjects = new List <TServiceObject>();
            TServiceObject        serviceObject  = null;

            object[] jsonServiceObjects = jsonResponse.ReadAsArray(collectionJsonElementName);
            foreach (object arrayEntry in jsonServiceObjects)
            {
                JsonObject jsonServiceObject = arrayEntry as JsonObject;

                if (jsonServiceObject != null)
                {
                    serviceObject = getObjectInstanceDelegate(this.Service, jsonServiceObject.ReadTypeString());

                    if (serviceObject != null)
                    {
                        if (string.Compare(jsonServiceObject.ReadTypeString(), serviceObject.GetXmlElementName(), StringComparison.Ordinal) != 0)
                        {
                            throw new ServiceLocalException(
                                      string.Format(
                                          "The type of the object in the store ({0}) does not match that of the local object ({1}).",
                                          jsonServiceObject.ReadTypeString(),
                                          serviceObject.GetXmlElementName()));
                        }

                        serviceObject.LoadFromJson(
                            jsonServiceObject,
                            this.Service,
                            clearPropertyBag,
                            requestedPropertySet,
                            summaryPropertiesOnly);

                        serviceObjects.Add(serviceObject);
                    }
                }
            }

            return(serviceObjects);
        }