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); } }
private void WriteElement(object o, ElementAccessor element, string arrayName, bool writeAccessor, XmlMapping parentMapping = null) { string name = writeAccessor ? element.Name : element.Mapping.TypeName; string ns = element.Any && element.Name.Length == 0 ? null : (element.Form == XmlSchemaForm.Qualified ? (writeAccessor ? element.Namespace : element.Mapping.Namespace) : ""); if (element.Mapping is NullableMapping) { if (o != null) { ElementAccessor e = element.Clone(); e.Mapping = ((NullableMapping)element.Mapping).BaseMapping; WriteElement(o, e, arrayName, writeAccessor); } else if (element.IsNullable) { WriteNullTagLiteral(element.Name, ns); } } else if (element.Mapping is ArrayMapping) { var mapping = (ArrayMapping)element.Mapping; if (mapping.IsSoap) { throw new PlatformNotSupportedException(); } if (element.IsNullable && o == null) { WriteNullTagLiteral(element.Name, element.Form == XmlSchemaForm.Qualified ? element.Namespace : ""); } else { if (element.IsUnbounded) { TypeDesc arrayTypeDesc = mapping.TypeDesc.CreateArrayTypeDesc(); var enumerable = (IEnumerable)o; foreach (var e in enumerable) { element.IsUnbounded = false; WriteElement(e, element, arrayName, writeAccessor); element.IsUnbounded = true; } } else { if (o != null) { WriteStartElement(name, ns, false); WriteArrayItems(mapping.ElementsSortedByDerivation, null, null, mapping.TypeDesc, o); WriteEndElement(); } } } } else if (element.Mapping is EnumMapping) { if (element.Mapping.IsSoap) { Writer.WriteStartElement(name, ns); WriteEnumMethod((EnumMapping)element.Mapping, o); WriteEndElement(); } else { WritePrimitive(WritePrimitiveMethodRequirement.WriteElementString, name, ns, element.Default, o, element.Mapping, false, true, element.IsNullable); } } else if (element.Mapping is PrimitiveMapping) { PrimitiveMapping mapping = (PrimitiveMapping)element.Mapping; if (mapping.TypeDesc == QnameTypeDesc) { WriteQualifiedNameElement(name, ns, element.Default, (XmlQualifiedName)o, element.IsNullable, mapping.IsSoap, mapping); } else { WritePrimitiveMethodRequirement suffixNullable = mapping.IsSoap ? WritePrimitiveMethodRequirement.Encoded : WritePrimitiveMethodRequirement.None; WritePrimitiveMethodRequirement suffixRaw = mapping.TypeDesc.XmlEncodingNotRequired ? WritePrimitiveMethodRequirement.Raw : WritePrimitiveMethodRequirement.None; WritePrimitive(element.IsNullable ? WritePrimitiveMethodRequirement.WriteNullableStringLiteral | suffixNullable | suffixRaw : WritePrimitiveMethodRequirement.WriteElementString | suffixRaw, name, ns, element.Default, o, mapping, mapping.IsSoap, true, element.IsNullable); } } else if (element.Mapping is StructMapping) { var mapping = (StructMapping)element.Mapping; if (mapping.IsSoap) { EnsureXmlSerializationWriteCallbackForMapping(mapping, name, ns, element.IsNullable, needType: false, parentMapping: parentMapping); WritePotentiallyReferencingElement(name, ns, o, !writeAccessor ? mapping.TypeDesc.Type : null, !writeAccessor, element.IsNullable); } else { WriteStructMethod(mapping, name, ns, o, element.IsNullable, needType: false, parentMapping: parentMapping); } } else if (element.Mapping is SpecialMapping) { if (element.Mapping is SerializableMapping) { WriteSerializable((IXmlSerializable)o, name, ns, element.IsNullable, !element.Any); } else { // XmlNode, XmlElement var node = o as XmlNode; if (node != null) { WriteElementLiteral(node, name, ns, element.IsNullable, element.Any); } else { throw CreateInvalidAnyTypeException(o); } } } else { throw new InvalidOperationException(SR.XmlInternalError); } }
private bool hasRequirement(WritePrimitiveMethodRequirement value, WritePrimitiveMethodRequirement requirement) { return (value & requirement) == requirement; }
private bool hasRequirement(WritePrimitiveMethodRequirement value, WritePrimitiveMethodRequirement requirement) { return((value & requirement) == requirement); }
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.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); } }