public LiteralAttributes(MemberInfo memberInfo, XmlAttributes xmlAtts) : this(memberInfo) { if (xmlAtts == null) { return; } Ignore = xmlAtts.XmlIgnore; if (!Ignore) { XmlChoiceIdentifier = xmlAtts.XmlChoiceIdentifier; XmlAttribute = xmlAtts.XmlAttribute; XmlArray = xmlAtts.XmlArray; XmlText = xmlAtts.XmlText; XmlEnum = xmlAtts.XmlEnum; DefaultValue = (xmlAtts.XmlDefaultValue == null) ? null : new DefaultValueAttribute(xmlAtts.XmlDefaultValue); Xmlns = xmlAtts.Xmlns; if (Xmlns) { object[] attrs = memberInfo.GetCustomAttributes(s_nsDeclType, false).ToArray(); XmlNamespaceDeclaration = (XmlNamespaceDeclarationsAttribute)(attrs.Length > 0 ? attrs[0] : null); } else { XmlNamespaceDeclaration = null; } // Use if statements here so that the XmlElements collection populated by reflection // is eliminated only if the app developer has provided substitute XmlElementAttribute's. // Ditto for the XmlArrayItems and XmlAnyElements. if (xmlAtts.XmlElements.Count > 0) { XmlElements = xmlAtts.XmlElements; } if (xmlAtts.XmlArrayItems.Count > 0) { XmlArrayItems = xmlAtts.XmlArrayItems; } XmlAnyAttribute = xmlAtts.XmlAnyAttribute; if (xmlAtts.XmlAnyElements.Count > 0) { XmlAnyElements = xmlAtts.XmlAnyElements; } } }
private XmlSerializer CreateOverrideSerializer() { // Override the Things field to capture all // unknown XML attributes. XmlAnyAttributeAttribute myAnyAttribute = new XmlAnyAttributeAttribute(); XmlAttributeOverrides xOverride = new XmlAttributeOverrides(); XmlAttributes xAtts = new XmlAttributes(); xAtts.XmlAnyAttribute = myAnyAttribute; xOverride.Add(typeof(Group), "Things", xAtts); return(new XmlSerializer(typeof(Group), xOverride)); }
/// <devdoc> /// <para>[To be supplied.]</para> /// </devdoc> public XmlAttributes(ICustomAttributeProvider provider) { object[] attrs = provider.GetCustomAttributes(false); // most generic <any/> matches everything XmlAnyElementAttribute wildcard = null; for (int i = 0; i < attrs.Length; i++) { if (attrs[i] is XmlIgnoreAttribute || attrs[i] is ObsoleteAttribute || attrs[i].GetType() == IgnoreAttribute) { _xmlIgnore = true; break; } else if (attrs[i] is XmlElementAttribute) { _xmlElements.Add((XmlElementAttribute)attrs[i]); } else if (attrs[i] is XmlArrayItemAttribute) { _xmlArrayItems.Add((XmlArrayItemAttribute)attrs[i]); } else if (attrs[i] is XmlAnyElementAttribute) { XmlAnyElementAttribute any = (XmlAnyElementAttribute)attrs[i]; if ((any.Name == null || any.Name.Length == 0) && any.NamespaceSpecified && any.Namespace == null) { // ignore duplicate wildcards wildcard = any; } else { _xmlAnyElements.Add((XmlAnyElementAttribute)attrs[i]); } } else if (attrs[i] is DefaultValueAttribute) { _xmlDefaultValue = ((DefaultValueAttribute)attrs[i]).Value; } else if (attrs[i] is XmlAttributeAttribute) { _xmlAttribute = (XmlAttributeAttribute)attrs[i]; } else if (attrs[i] is XmlArrayAttribute) { _xmlArray = (XmlArrayAttribute)attrs[i]; } else if (attrs[i] is XmlTextAttribute) { _xmlText = (XmlTextAttribute)attrs[i]; } else if (attrs[i] is XmlEnumAttribute) { _xmlEnum = (XmlEnumAttribute)attrs[i]; } else if (attrs[i] is XmlRootAttribute) { _xmlRoot = (XmlRootAttribute)attrs[i]; } else if (attrs[i] is XmlTypeAttribute) { _xmlType = (XmlTypeAttribute)attrs[i]; } else if (attrs[i] is XmlAnyAttributeAttribute) { _xmlAnyAttribute = (XmlAnyAttributeAttribute)attrs[i]; } else if (attrs[i] is XmlChoiceIdentifierAttribute) { _xmlChoiceIdentifier = (XmlChoiceIdentifierAttribute)attrs[i]; } else if (attrs[i] is XmlNamespaceDeclarationsAttribute) { _xmlns = true; } } if (_xmlIgnore) { _xmlElements.Clear(); _xmlArrayItems.Clear(); _xmlAnyElements.Clear(); _xmlDefaultValue = null; _xmlAttribute = null; _xmlArray = null; _xmlText = null; _xmlEnum = null; _xmlType = null; _xmlAnyAttribute = null; _xmlChoiceIdentifier = null; _xmlns = false; } else { if (wildcard != null) { _xmlAnyElements.Add(wildcard); } } }
public LiteralAttributes(MemberInfo memberInfo) { foreach (object attr in memberInfo.GetCustomAttributes(false)) { Type type = attr.GetType(); if (type == s_ignoreType) { Debug.Assert(false == Ignore, "Too many ignores"); Ignore = true; } else if (type == s_choiceType) { Debug.Assert(XmlChoiceIdentifier == null, "Too many XCIA"); XmlChoiceIdentifier = (XmlChoiceIdentifierAttribute)attr; } else if (type == s_elementType) { if (XmlElements == null) { XmlElements = new XmlElementAttributes(); } XmlElements.Add((XmlElementAttribute)attr); } else if (type == s_attributeType) { Debug.Assert(XmlAttribute == null, "Too many XAAs"); XmlAttribute = (XmlAttributeAttribute)attr; } else if (type == s_arrayType) { Debug.Assert(XmlArray == null, "Too many XAAs"); XmlArray = (XmlArrayAttribute)attr; } else if (type == s_arrayItemType) { if (XmlArrayItems == null) { XmlArrayItems = new XmlArrayItemAttributes(); } XmlArrayItems.Add((XmlArrayItemAttribute)attr); } else if (type == s_nsDeclType) { Debug.Assert(XmlNamespaceDeclaration == null, "Too many XNDAs"); XmlNamespaceDeclaration = (XmlNamespaceDeclarationsAttribute)attr; Xmlns = true; } else if (type == s_textType) { Debug.Assert(XmlText == null, "Too many XTAs"); XmlText = (XmlTextAttribute)attr; } else if (type == s_anyAttrType) { Debug.Assert(XmlAnyAttribute == null, "Too many XAAAs"); XmlAnyAttribute = (XmlAnyAttributeAttribute)attr; } else if (type == s_anyEltType) { if (XmlAnyElements == null) { XmlAnyElements = new XmlAnyElementAttributes(); } XmlAnyElements.Add((XmlAnyElementAttribute)attr); } else if (type == s_enumType) { XmlEnum = (XmlEnumAttribute)attr; } else { base.processAttribute(attr); } } }