예제 #1
0
        private static void ParseObjectField(object result, XmlElement childElement, XmlProcessingContext xmlProcessingContext)
        {
            if (childElement.Attributes["name"] == null)
            {
                xmlProcessingContext.ReportError("'Field' element in the composition XML should have a 'name' attribute.");
                return;
            }

            var fieldName = childElement.Attributes["name"].Value;

            xmlProcessingContext.EnterRunningLocation(string.Format("Field({0})", fieldName));

            var fieldInfo = result.GetType().GetField(fieldName);

            if (fieldInfo == null)
            {
                xmlProcessingContext.ReportError(
                    string.Format("Object type '{0}' does not contain a field definition named '{1}", result.GetType().FullName,
                                  fieldName));
                xmlProcessingContext.LeaveRunningLocation();
                return;
            }

            var fieldValue = XmlValueParser.ParseValue(childElement, xmlProcessingContext, objectFieldExcludedAttributes);

            fieldInfo.SetValue(result, fieldValue);

            xmlProcessingContext.LeaveRunningLocation();
        }
        private static void ParseDictionaryItem(IDictionary result, XmlElement childElement,
                                                XmlProcessingContext xmlProcessingContext)
        {
            if (childElement.GetElementsByTagName("Key").Count != 1)
            {
                xmlProcessingContext.ReportError(
                    "Each 'Item' in a 'Dictionary' should contain exactly one 'Key' element as a nested tag.");
                return;
            }

            if (childElement.GetElementsByTagName("Value").Count != 1)
            {
                xmlProcessingContext.ReportError(
                    "Each 'Item' in a 'Dictionary' should contain exactly one 'Value' element as a nested tag.");
                return;
            }

            xmlProcessingContext.EnterRunningLocation("Key");
            var keyValue =
                XmlValueParser.ParseValue((XmlElement)childElement.GetElementsByTagName("Key")[0], xmlProcessingContext, null);

            xmlProcessingContext.LeaveRunningLocation();

            xmlProcessingContext.EnterRunningLocation("Value");
            var valueValue =
                XmlValueParser.ParseValue((XmlElement)childElement.GetElementsByTagName("Value")[0], xmlProcessingContext, null);

            xmlProcessingContext.LeaveRunningLocation();

            result.Add(keyValue, valueValue);
        }
        /// <summary>
        /// Parses a SerializeBinary element in the composition XML.
        /// </summary>
        public static object ParseSerializeBinary(XmlElement element, XmlProcessingContext context)
        {
            context.EnterRunningLocation("SerializeBinary");

            var serializableValue = XmlValueParser.ParseValue(element, context, null);

            var formatter = new BinaryFormatter();
            var stream    = new MemoryStream();

            formatter.Serialize(stream, serializableValue);

            var result = stream.ToArray();

            stream.Close();

            context.LeaveRunningLocation();

            return(result);
        }
        private static List <object> GetCollectionElements(XmlElement[] xmlElements, Type elementType,
                                                           XmlProcessingContext xmlProcessingContext)
        {
            var arrayElements = new List <object>();

            for (var i = 0; i < xmlElements.Length; i++)
            {
                xmlProcessingContext.EnterRunningLocation(string.Format("Item({0})", i));

                var childElement = xmlElements[i];

                if (childElement.Name == "Item")
                {
                    var arrayElement = XmlValueParser.ParseValue(childElement, xmlProcessingContext, null);

                    if ((arrayElement != null) && (!elementType.IsAssignableFrom(arrayElement.GetType())))
                    {
                        xmlProcessingContext.ReportError(
                            string.Format("Array of the element type {0} can not contain items with type {1}.", elementType.FullName,
                                          arrayElement.GetType().FullName));
                        xmlProcessingContext.LeaveRunningLocation();
                        return(null);
                    }

                    arrayElements.Add(arrayElement);
                }
                else
                {
                    xmlProcessingContext.ReportError(
                        string.Format("Xml element '{0}' is not allowed in 'Array' element - type: {1}[]", childElement.Name,
                                      elementType.FullName));
                    xmlProcessingContext.LeaveRunningLocation();
                    return(null);
                }

                xmlProcessingContext.LeaveRunningLocation();
            }

            return(arrayElements);
        }
예제 #5
0
        private static object[] ParseConstructorArgs(XmlElement element,
                                                     XmlProcessingContext xmlProcessingContext)
        {
            if (element.Name != "ConstructorArgs")
            {
                throw new ArgumentException("Calling this method is only valid for 'ConstructorArgs' element.");
            }

            if (element.HasAttributes)
            {
                xmlProcessingContext.ReportError("'ConstructorArgs' element should not have any attributes.");
            }

            xmlProcessingContext.EnterRunningLocation("ConstructorArgs");

            var result = new List <object>();

            foreach (XmlNode childNode in element.ChildNodes)
            {
                var childElement = childNode as XmlElement;
                if (childElement == null)
                {
                    continue;
                }

                if (childElement.Name == "Arg")
                {
                    result.Add(XmlValueParser.ParseValue(childElement, xmlProcessingContext, null));
                }
                else
                {
                    xmlProcessingContext.ReportError(
                        string.Format("Element '{0}' is not allowed in the 'ConstructorArgs' element.", childElement.Name));
                }
            }

            xmlProcessingContext.LeaveRunningLocation();
            return(result.ToArray());
        }