private void WriteQualifiedNameElement(string name, string ns, object defaultValue, XmlQualifiedName o, bool nullable, bool isSoap, PrimitiveMapping mapping) { bool hasDefault = defaultValue != null && !Globals.IsDBNullValue(defaultValue) && mapping.TypeDesc.HasDefaultSupport; if (hasDefault && IsDefaultValue(mapping, o, defaultValue, nullable)) { return; } if (isSoap) { throw new PlatformNotSupportedException(); } if (nullable) { WriteNullableQualifiedNameLiteral(name, ns, o); } else { WriteElementQualifiedName(name, ns, o); } }
private void WriteElement(ref object o, CollectionMember collectionMember, ElementAccessor element, ChoiceIdentifierAccessor choice, bool checkSpecified, bool checkForNull, bool readOnly, string defaultNamespace, object masterObject = null) { object value = null; if (element.Mapping is ArrayMapping) { WriteArray(ref value, (ArrayMapping)element.Mapping, readOnly, element.IsNullable, defaultNamespace); } else if (element.Mapping is NullableMapping) { value = WriteNullableMethod((NullableMapping)element.Mapping, true, defaultNamespace); } else if (!element.Mapping.IsSoap && (element.Mapping is PrimitiveMapping)) { if (element.IsNullable && ReadNull()) { if (element.Mapping.TypeDesc.IsValueType) { value = ReflectionCreateObject(element.Mapping.TypeDesc.Type); } else { value = null; } } else if ((element.Default != null && !Globals.IsDBNullValue(element.Default) && element.Mapping.TypeDesc.IsValueType) && (Reader.IsEmptyElement)) { Reader.Skip(); } else { if (element.Mapping.TypeDesc == QnameTypeDesc) { value = ReadElementQualifiedName(); } else { if (element.Mapping.TypeDesc.FormatterName == "ByteArrayBase64") { value = ToByteArrayBase64(false); } else if (element.Mapping.TypeDesc.FormatterName == "ByteArrayHex") { value = ToByteArrayHex(false); } else { Func <string> readFunc = () => Reader.ReadElementContentAsString(); value = WritePrimitive(element.Mapping, readFunc); } } } } else if (element.Mapping is StructMapping || (element.Mapping.IsSoap && element.Mapping is PrimitiveMapping)) { TypeMapping mapping = element.Mapping; if (mapping.IsSoap) { throw new PlatformNotSupportedException(); } else { if (checkForNull && o == null) { Reader.Skip(); } else { value = WriteStructMethod( mapping: (StructMapping)mapping, isNullable: mapping.TypeDesc.IsNullable && element.IsNullable, checkType: true, defaultNamespace: defaultNamespace ); } } } else if (element.Mapping is SpecialMapping) { SpecialMapping special = (SpecialMapping)element.Mapping; switch (special.TypeDesc.Kind) { case TypeKind.Node: bool isDoc = special.TypeDesc.FullName == typeof(XmlDocument).FullName; if (isDoc) { value = ReadXmlDocument(!element.Any); } else { value = ReadXmlNode(!element.Any); } break; case TypeKind.Serializable: SerializableMapping sm = (SerializableMapping)element.Mapping; // check to see if we need to do the derivation bool flag = true; if (sm.DerivedMappings != null) { XmlQualifiedName tser = GetXsiType(); if (tser == null || QNameEqual(tser, sm.XsiType.Name, sm.XsiType.Namespace, defaultNamespace)) { } else { flag = false; } } if (flag) { bool isWrappedAny = !element.Any && IsWildcard(sm); value = ReadSerializable((IXmlSerializable)ReflectionCreateObject(sm.TypeDesc.Type), isWrappedAny); } if (sm.DerivedMappings != null) { // #10587: To Support SpecialMapping Types Having DerivedMappings throw new NotImplementedException("sm.DerivedMappings != null"); //WriteDerivedSerializable(sm, sm, source, isWrappedAny); //WriteUnknownNode("UnknownNode", "null", null, true); } break; default: throw new InvalidOperationException(SR.Format(SR.XmlInternalError)); } } else { throw new InvalidOperationException(SR.Format(SR.XmlInternalError)); } if (choice != null && masterObject != null) { foreach (var name in choice.MemberIds) { if (name == element.Name) { object choiceValue = Enum.Parse(choice.Mapping.TypeDesc.Type, name); SetOrAddValueToMember(masterObject, choiceValue, choice.MemberInfo); break; } } } if (collectionMember != null) { collectionMember.Add(value); } else { o = value; } }
private void WritePrimitive(WritePrimitiveMethodRequirement method, string name, string ns, object defaultValue, object o, TypeMapping mapping, bool writeXsiType, bool isElement, bool isNullable) { TypeDesc typeDesc = mapping.TypeDesc; bool hasDefault = defaultValue != null && !Globals.IsDBNullValue(defaultValue) && mapping.TypeDesc.HasDefaultSupport; if (hasDefault) { if (mapping is EnumMapping) { if (((EnumMapping)mapping).IsFlags) { var defaultEnumFlagValues = defaultValue.ToString().Split(null).Where((s) => !string.IsNullOrWhiteSpace(s)); string defaultEnumFlagString = string.Join(", ", defaultEnumFlagValues); if (o.ToString() == defaultEnumFlagString) { return; } } else { if (o.ToString() == defaultValue.ToString()) { return; } } } else { if (IsDefaultValue(mapping, o, defaultValue, isNullable)) { return; } } } XmlQualifiedName xmlQualifiedName = null; if (writeXsiType) { xmlQualifiedName = new XmlQualifiedName(mapping.TypeName, mapping.Namespace); } string stringValue = null; bool hasValidStringValue = false; if (mapping is EnumMapping) { stringValue = WriteEnumMethod((EnumMapping)mapping, o); hasValidStringValue = true; } else { hasValidStringValue = WritePrimitiveValue(typeDesc, o, isElement, out stringValue); } if (hasValidStringValue) { if (hasRequirement(method, WritePrimitiveMethodRequirement.WriteElementString)) { if (hasRequirement(method, WritePrimitiveMethodRequirement.Raw)) { WriteElementString(name, ns, stringValue, xmlQualifiedName); } else { WriteElementStringRaw(name, ns, stringValue, xmlQualifiedName); } } else if (hasRequirement(method, WritePrimitiveMethodRequirement.WriteNullableStringLiteral)) { if (hasRequirement(method, WritePrimitiveMethodRequirement.Encoded)) { if (hasRequirement(method, WritePrimitiveMethodRequirement.Raw)) { WriteNullableStringEncoded(name, ns, stringValue, xmlQualifiedName); } else { WriteNullableStringEncodedRaw(name, ns, stringValue, xmlQualifiedName); } } else { if (hasRequirement(method, WritePrimitiveMethodRequirement.Raw)) { WriteNullableStringLiteral(name, ns, stringValue); } else { WriteNullableStringLiteralRaw(name, ns, stringValue); } } } else if (hasRequirement(method, WritePrimitiveMethodRequirement.WriteAttribute)) { WriteAttribute(name, ns, stringValue); } else { // #10593: Add More Tests for Serialization Code Debug.Assert(false); } } else if (o is byte[]) { byte[] a = (byte[])o; if (hasRequirement(method, WritePrimitiveMethodRequirement.WriteElementString | WritePrimitiveMethodRequirement.Raw)) { WriteElementStringRaw(name, ns, FromByteArrayBase64(a)); } else if (hasRequirement(method, WritePrimitiveMethodRequirement.WriteNullableStringLiteral | WritePrimitiveMethodRequirement.Raw)) { WriteNullableStringLiteralRaw(name, ns, FromByteArrayBase64(a)); } else if (hasRequirement(method, WritePrimitiveMethodRequirement.WriteAttribute)) { WriteAttribute(name, ns, a); } else { // #10593: Add More Tests for Serialization Code Debug.Assert(false); } } else { // #10593: Add More Tests for Serialization Code Debug.Assert(false); } }