private void ProcessPrimitiveProperty(XElement parentElement, object data, PropertyInfo propertyInfo) { if (!CheckShouldSerialize(data, propertyInfo.Name)) { return; } XName name = GetElementName(propertyInfo); object content = propertyInfo.GetGetMethod().Invoke(data, null); object defaultValue = GetElementDefaultValue(propertyInfo); if (Equals(content, defaultValue)) { return; } XmlTextAttribute attribute = propertyInfo.GetCustomAttribute <XmlTextAttribute>(); if (attribute != null) { parentElement.SetValue(content); } else { parentElement.Add(new XElement(name, content)); } }
public void DataTypeDefault() { XmlTextAttribute attr = new XmlTextAttribute(); Assert.AreEqual(string.Empty, attr.DataType, "#1"); attr.DataType = null; Assert.AreEqual(string.Empty, attr.DataType, "#2"); }
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; } } }
public void XmlTextAttribute() { // based on default ctor. XmlTextAttribute attr = new XmlTextAttribute(); Assert.AreEqual("", attr.DataType, "#1"); Assert.IsNull(attr.Type, "#2"); // based on a type. XmlTextAttribute attr2 = new XmlTextAttribute(typeof(XmlNode)); Assert.AreEqual("", attr.DataType, "#3"); Assert.IsNull(attr.Type, "#4"); }
private static void AddXmlTextPrint(XmlTextAttribute att, StringBuilder printBuilder) { if (null != att) { printBuilder.Append(att.DataType); printBuilder.Append("/"); if (null != att.Type) { printBuilder.Append(att.Type.AssemblyQualifiedName); } } printBuilder.Append("%%"); }
public void DifferentTypes() { XmlTextAttribute text1 = new XmlTextAttribute(typeof(SerializeMe)); XmlTextAttribute text2 = new XmlTextAttribute(typeof(SerializeMeToo)); atts1.XmlText = text1; atts2.XmlText = text2; ov1.Add(typeof(SerializeMe), atts1); ov2.Add(typeof(SerializeMe), atts2); ThumbprintHelpers.DifferentThumbprint(ov1, ov2); }
public void SameType() { XmlTextAttribute text1 = new XmlTextAttribute(typeof(SerializeMe)); XmlTextAttribute text2 = new XmlTextAttribute(typeof(SerializeMe)); atts1.XmlText = text1; atts2.XmlText = text2; ov1.Add(typeof(SerializeMe), atts1); ov2.Add(typeof(SerializeMe), atts2); ThumbprintHelpers.SameThumbprint(ov1, ov2); }
public void DifferentDataTypes() { XmlTextAttribute text1 = new XmlTextAttribute(); text1.DataType = "xmltype1"; XmlTextAttribute text2 = new XmlTextAttribute(); text2.DataType = "xmltype2"; atts1.XmlText = text1; atts2.XmlText = text2; ov1.Add(typeof(SerializeMe), atts1); ov2.Add(typeof(SerializeMe), atts2); ThumbprintHelpers.DifferentThumbprint(ov1, ov2); }
// Return an XmlSerializer to be used for overriding. public XmlSerializer CreateOverrider() { // Create the XmlAttributeOverrides and XmlAttributes objects. XmlAttributeOverrides xOver = new XmlAttributeOverrides(); XmlAttributes xAttrs = new XmlAttributes(); /* Create an XmlTextAttribute and assign it to the XmlText * property. This instructs the XmlSerializer to treat the * Comment field as XML text. */ XmlTextAttribute xText = new XmlTextAttribute(); xAttrs.XmlText = xText; xOver.Add(typeof(Group), "Comment", xAttrs); // Create the XmlSerializer, and return it. return(new XmlSerializer(typeof(Group), xOver)); }
/* Create an instance of the XmlSerializer class that overrides * the default way it serializes an object. */ public XmlSerializer CreateOverrider() { /* Create instances of the XmlAttributes and * XmlAttributeOverrides classes. */ XmlAttributes attrs = new XmlAttributes(); XmlAttributeOverrides xOver = new XmlAttributeOverrides(); /* Create an XmlTextAttribute to override the default * serialization process. */ XmlTextAttribute xText = new XmlTextAttribute(); attrs.XmlText = xText; // Add the XmlAttributes to the XmlAttributeOverrides. xOver.Add(typeof(Group), "Comment", attrs); // Create the XmlSerializer, and return it. XmlSerializer xSer = new XmlSerializer(typeof(Group), xOver); return(xSer); }
/// <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); } } }
private static void FillHashSourceList(List <string> dict, object obj, string parentproperty) { if (obj == null) { return; } //Console.WriteLine("Enter in " + obj.ToString()); PropertyDescriptorCollection pdc = TypeDescriptor.GetProperties(obj); Type type = obj.GetType(); foreach (PropertyDescriptor p in pdc) { string propname = "/" + p.Name; object prop = p.GetValue(obj); if (prop != null) { PropertyInfo pi = type.GetProperty(p.Name); foreach (Attribute a in Attribute.GetCustomAttributes(pi)) { XmlElementAttribute ea = a as XmlElementAttribute; if (ea != null) { if (prop.GetType() == ea.Type) { propname = "/" + ea.ElementName; break; } } XmlTextAttribute ta = a as XmlTextAttribute; if (ta != null && p.Name == "Value") { propname = ""; break; } XmlAttributeAttribute aa = a as XmlAttributeAttribute; if (aa != null) { propname = "@" + p.Name; } } string name = parentproperty + propname; if (strValue(prop) != "" && hasXmlAttr(p)) { if (p.PropertyType.IsArray) { int i = 0; foreach (object propitem in prop as Array) { i++; FillHashSourceList(dict, propitem, name + "[" + i.ToString() + "]"); } } else { string value = strValue(prop); if (isValue(prop)) { //Console.WriteLine("---"+name + "\t" + value); dict.Add(name + "\t" + value); } else { FillHashSourceList(dict, prop, name); } } } } } }
/// <summary> /// Adds new attribute to the node decorations. /// </summary> /// <param name="attr">An attriubute.</param> protected void Add(object attr) { if (attr is NodePolicyAttribute) { if (_nodePolicy == null) { _nodePolicy = (NodePolicyAttribute)attr; } } else if (attr is ConverterAttribute) { if (_converter == null) { _converter = (ConverterAttribute)attr; } } else if (attr is TransparentAttribute) { _transparent = (TransparentAttribute)attr; } else if (attr is SkipNavigableRootAttribute) { _skipNavigableRoot = (SkipNavigableRootAttribute)attr; } else if (attr is ChildXmlElementAttribute) { _childXmlElementName = (ChildXmlElementAttribute)attr; } else if (attr is XmlRootAttribute) { if (_xmlRoot == null) { _xmlRoot = (XmlRootAttribute)attr; } } else if (attr is XmlAttributeAttribute) { if (NodeTypeUndefined()) { _xmlAttribute = (XmlAttributeAttribute)attr; } } else if (attr is XmlElementAttribute) { if (NodeTypeUndefined()) { _xmlElement = (XmlElementAttribute)attr; _isNullable = _xmlElement.IsNullable; } } else if (attr is XmlAnyElementAttribute) { if (NodeTypeUndefined()) { _xmlAnyElement = (XmlAnyElementAttribute)attr; } } else if (attr is XmlTextAttribute) { if (NodeTypeUndefined()) { _xmlText = (XmlTextAttribute)attr; } } else if (attr is XmlIgnoreAttribute) { _xmlIgnore = (XmlIgnoreAttribute)attr; } else if (attr is XmlTypeAttribute) { _xmlType = (XmlTypeAttribute)attr; } }
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); } } }
public void TypeDefault() { XmlTextAttribute attr = new XmlTextAttribute(); Assert.IsNull(attr.Type); }