internal static XmlQualifiedEntity LookupXmlStartElement(this IHierarchy <object> node, XmlQualifiedEntity qualifiedRootEntity = null)
        {
            if (node == null)
            {
                throw new ArgumentNullException(nameof(node));
            }
            if (qualifiedRootEntity != null && !qualifiedRootEntity.LocalName.IsNullOrWhiteSpace())
            {
                return(qualifiedRootEntity);
            }
            bool   hasRootAttribute    = TypeUtility.ContainsAttributeType(node.InstanceType, true, typeof(XmlRootAttribute));
            bool   hasElementAttribute = node.HasMemberReference && TypeUtility.ContainsAttributeType(node.MemberReference, typeof(XmlElementAttribute));
            string rootOrElementName   = XmlUtility.SanitizeElementName(node.HasMemberReference ? node.MemberReference.Name : StringConverter.FromType(node.InstanceType, false, true));
            string ns = null;

            if (hasRootAttribute || hasElementAttribute)
            {
                string elementName = null;
                if (hasRootAttribute)
                {
                    XmlRootAttribute rootAttribute = node.InstanceType.GetTypeInfo().GetCustomAttribute <XmlRootAttribute>(true);
                    elementName = rootAttribute.ElementName;
                    ns          = rootAttribute.Namespace;
                }

                if (hasElementAttribute)
                {
                    XmlElementAttribute elementAttribute = node.MemberReference.GetCustomAttribute <XmlElementAttribute>();
                    elementName = elementAttribute.ElementName;
                    ns          = elementAttribute.Namespace;
                }

                if (!string.IsNullOrEmpty(elementName))
                {
                    rootOrElementName = elementName;
                }
            }

            XmlQualifiedEntity instance = node.Instance as XmlQualifiedEntity;

            return(instance ?? new XmlQualifiedEntity(XmlUtility.SanitizeElementName(rootOrElementName), ns));
        }
Beispiel #2
0
        private void WriteXmlValue(XmlWriter writer, IHierarchy <object> node)
        {
            if (node.IsNodeEnumerable())
            {
                return;
            }

            var converter = Converters.FirstOrDefaultWriterConverter(node.InstanceType);

            if (converter != null)
            {
                converter.WriteXml(writer, node.Instance);
                return;
            }

            bool hasAttributeAttribute = node.HasMemberReference && TypeUtility.ContainsAttributeType(node.MemberReference, typeof(XmlAttributeAttribute));
            bool hasElementAttribute   = node.HasMemberReference && TypeUtility.ContainsAttributeType(node.MemberReference, typeof(XmlElementAttribute));
            bool hasTextAttribute      = node.HasMemberReference && TypeUtility.ContainsAttributeType(node.MemberReference, typeof(XmlTextAttribute));

            bool   isType   = node.Instance is Type;
            Type   nodeType = isType ? (Type)node.Instance : node.InstanceType;
            string attributeOrElementName = XmlUtility.SanitizeElementName(node.HasMemberReference ? node.MemberReference.Name : StringConverter.FromType(nodeType));

            if (!hasAttributeAttribute && !hasElementAttribute && !hasTextAttribute)
            {
                hasElementAttribute = true; // default serialization value for legacy Cuemon
                if ((!TypeUtility.IsComplex(nodeType) || isType) && !node.HasChildren && (isType))
                {
                    if (!node.HasParent)
                    {
                        hasElementAttribute = false;
                    }
                    hasTextAttribute = true;
                }
            }
            else
            {
                string elementName = null;
                if (hasAttributeAttribute)
                {
                    elementName = node.MemberReference.GetCustomAttribute <XmlAttributeAttribute>().AttributeName;
                }
                if (hasElementAttribute)
                {
                    elementName = node.MemberReference.GetCustomAttribute <XmlElementAttribute>().ElementName;
                }

                if (!string.IsNullOrEmpty(elementName))
                {
                    attributeOrElementName = elementName;
                }
            }

            var value = Wrapper.ParseInstance(node);

            if (hasAttributeAttribute)
            {
                writer.WriteAttributeString(attributeOrElementName, value);
            }
            else if (hasElementAttribute)
            {
                if (node.HasMemberReference)
                {
                    writer.WriteElementString(attributeOrElementName, value);
                }
                else
                {
                    writer.WriteValue(value);
                }
            }
            else if (hasTextAttribute)
            {
                writer.WriteString(value);
            }
        }
Beispiel #3
0
 /// <summary>
 /// Determines whether the specified source type contains one or more of the specified attribute target types.
 /// </summary>
 /// <param name="source">The member to match against.</param>
 /// <param name="inherit"><c>true</c> to search the <paramref name="source"/> inheritance chain to find the attributes; otherwise, <c>false</c>.</param>
 /// <param name="targets">The attribute target types to be matched against.</param>
 /// <returns>
 ///     <c>true</c> if the specified member contains one or more of the specified attribute target types; otherwise, <c>false</c>.
 /// </returns>
 public static bool HasAttributes(this MemberInfo source, bool inherit, params Type[] targets)
 {
     return(TypeUtility.ContainsAttributeType(source, inherit, targets));
 }