예제 #1
0
        /// <summary>
        /// Using childNodes from an element, deserialize an array by deserializing the
        /// individual elements into the array elements.
        /// </summary>
        /// <param name="_xml">
        /// The XML parent node containing the array elements as childNodes
        /// </param>
        /// <param name="_arrayHelper">The ArrayDeserializationHelper</param>
        private void DeserializeArrayFromElements(XmlElement _xml, CArrayDeserializationHelper _arrayHelper)
        {
            foreach (XmlNode node in _xml.ChildNodes)
            {
                if (!(node is XmlElement elem))
                {
                    throw new XDeserializationError(node.NodeType.ToString() +
                                                    " was the nodetype found (instead of XmlElement) as a child of the array's XML");
                }

                int[] indicies = null;
                var   sIndex   = XmlExtensions.GetAttributeValue(elem, m_context.ArrayIndexAttributeName);
                if (sIndex != null)
                {
                    indicies = sIndex.Split(',').Select(s => int.Parse(s)).ToArray();
                }

                if (indicies != null && indicies.Length > 0)
                {
                    _arrayHelper.SetIndicies(indicies);
                }

                var obj = FrameworkDeserialize(elem, _arrayHelper.ElementType);
                _arrayHelper.Add(obj);
            }
        }
예제 #2
0
        /// <summary>
        /// Given information about the current state, create an ArrayDeserializationHelper to
        /// help with the deserialization process
        /// </summary>
        /// <param name="_xml">The XML containing the Array information</param>
        /// <param name="_type">The Type of the array expected</param>
        /// <param name="_workingObject">
        /// A "Working Object", likely from a surrogate that didn't complete the deserialization
        /// </param>
        /// <param name="_inferredLength">
        /// The Inferred Length of the array, from a "Condensed Array" already found in the XML
        /// </param>
        /// <returns>
        /// A properly formed <see cref="CArrayDeserializationHelper"/> object.
        /// </returns>
        private CArrayDeserializationHelper CreateArrayDeserializationHelper(XmlElement _xml,
                                                                             Type _type,
                                                                             CWorkingObject _workingObject,
                                                                             int _inferredLength)
        {
            CArrayDeserializationHelper arrayHelper;

            if (_workingObject.IsSet)
            {
                arrayHelper = new CArrayDeserializationHelper(_workingObject.WorkingObject as Array);
            }
            else
            {
                // Make sure that the array type actually is an array and it has an ElementType
                var arrType = _type;
                if (!arrType.IsArray)
                {
                    throw new XDeserializationError(
                              "The Type specified is not an array or it does not have an ElementTypes associated with it- " +
                              arrType.ToString());
                }

                // Get info from the XML
                var sLengths = XmlExtensions.GetAttributeValue(_xml, m_context.ArrayAttributeName);
                var lengths  = sLengths?.Split(',').Select(s => int.Parse(s)).ToArray();

                var sLowerBounds = XmlExtensions.GetAttributeValue(_xml, m_context.ArrayLowerBoundAttribute);
                var lowerBounds  = sLowerBounds?.Split(',').Select(s => int.Parse(s)).ToArray();

                if (lengths == null)
                {
                    if (_inferredLength > -1)
                    {
                        lengths = new int[] { _inferredLength }
                    }
                    ;
                    else
                    {
                        lengths = new int[] { InferArrayLength(_xml) }
                    };                                                    // This assumes a 1-dim array at this point.
                }

                arrayHelper = new CArrayDeserializationHelper(arrType.GetElementType(), lengths, lowerBounds);
            }

            return(arrayHelper);
        }
예제 #3
0
        /// <summary>
        /// Using the info in a "condensed array", deserialize an Array into an object.
        /// </summary>
        /// <param name="_condensedArray">
        /// The "Condensed Array"- a string[] containing data from a CSV list
        /// </param>
        /// <param name="_arrayHelper">The ArrayDeserializationHelper</param>
        private static void DeserializeArrayFromCondensedArray(string[] _condensedArray,
                                                               CArrayDeserializationHelper _arrayHelper)
        {
            var elemType = _arrayHelper.ElementType;

            foreach (var s in _condensedArray)
            {
                if (elemType == TYPEOF_STRING)
                {
                    var str = UnprotectStringFromStringlist(s);
                    _arrayHelper.Add(str);
                }
                else if (elemType.IsPrimitive)
                {
                    var obj = Convert.ChangeType(s, elemType);
                    _arrayHelper.Add(obj);
                }
                else
                {
                    throw new XDeserializationError(
                              "A single XmlText node was found, but the ElementType is complex- " + elemType.ToString());
                }
            }
        }