Ejemplo n.º 1
0
        /// <summary>
        /// Read child element of the current node
        /// </summary>
        /// <param name="context">XML context</param>
        /// <param name="reader">XML reader</param>
        /// <param name="setToProperty">Property to which the object must be assigned, or null for automatic resolution</param>
        protected virtual void ReadChildElement(IXsContext context, XmlReader reader, PropertyInfo setToProperty)
        {
            Type         t;
            PropertyInfo collProperty = null;

            if (setToProperty != null)
            {
                t = setToProperty.PropertyType;
            }
            else
            {
                ResolveCollectionAndTypeForNode(reader, context, out collProperty, out t);
            }

            object     newObject = Utils.CreateInstance(t);
            IXsElement xse       = newObject as IXsElement;

            if (xse != null)
            {
                xse.ReadXml(context, reader);
            }
            else
            {
                reader.Skip();
            }

            SetChildObject(reader, newObject, setToProperty, collProperty);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Write element to the output stream
        /// </summary>
        /// <param name="writer">Where to write</param>
        /// <param name="nameOverride">Local name to be used, or null if name should be retirevent from <see cref="XsTypeAttribute"/> of the type.</param>
        public virtual void WriteXml(XmlWriter writer, string nameOverride)
        {
            string namesp = null;

            if (nameOverride == null)
            {
                foreach (XsTypeAttribute a in CustomAttributeHelper.All <XsTypeAttribute>(GetType()))
                {
                    nameOverride = a.Name;
                    namesp       = a.Namespace;
                    break;
                }
                if (nameOverride == null)
                {
                    return;
                }
            }
            writer.WriteStartElement(nameOverride, namesp);

            WriteAttributes(writer);
            WriteText(writer);

            foreach (PropertyInfo c in GetOrderedElementProperties(GetType()))
            {
                object             v  = c.GetValue(this, null);
                IXsElement         o  = v as IXsElement;
                XsElementAttribute ab = CustomAttributeHelper.First <XsElementAttribute>(c);
                if (ab == null)
                {
                    continue;
                }
                if (ab.Name.Length == 0)
                {
                    IEnumerable e = (v as IEnumerable);
                    if (e != null)
                    {
                        foreach (IXsElement action in e)
                        {
                            if (action != null)
                            {
                                action.WriteXml(writer, ab.CollectionItemElementName);
                            }
                        }
                    }
                }
                else if (o != null && !(ab.SkipIfEmpty && ab.IsEmpty(v)))
                {
                    o.WriteXml(writer, ab.Name);
                }
            }

            writer.WriteEndElement();
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Read child element of the current node
        /// </summary>
        /// <param name="context">XML context</param>
        /// <param name="reader">XML reader</param>
        /// <param name="setToProperty">Property to which the object must be assigned, or null for automatic resolution</param>
        protected override void ReadChildElement(IXsContext context, XmlReader reader, PropertyInfo setToProperty)
        {
            Type t;

            if (setToProperty != null)
            {
                t = setToProperty.PropertyType;
            }
            else
            {
                t = (context == null)?null:context.ResolveType(reader);
            }
            if (t == null)
            {
                throw new XsException(reader, string.Format("Unknown xml element '{0}'", reader.Name));
            }

            object     newObject = Utils.CreateInstance(t);
            IXsElement xse       = newObject as IXsElement;

            if (xse != null)
            {
                xse.ReadXml(context, reader);
            }
            else
            {
                reader.Skip();
            }

            if (setToProperty != null)
            {
                SetChildObject(reader, newObject, setToProperty, null);
            }
            else
            {
                Add((IScriptAction)newObject);
            }
        }