internal override bool Equals(object other, Dictionary <DataContractPairKey, object> checkedContracts) { if (IsEqualOrChecked(other, checkedContracts)) { return(true); } XmlDataContract dataContract = other as XmlDataContract; if (dataContract != null) { if (this.HasRoot != dataContract.HasRoot) { return(false); } if (this.IsAnonymous) { return(dataContract.IsAnonymous); } else { return(StableName.Name == dataContract.StableName.Name && StableName.Namespace == dataContract.StableName.Namespace); } } return(false); }
internal object ReadIXmlSerializable(XmlReaderDelegator xmlReader, XmlDataContract xmlDataContract, bool isMemberType) { if (_xmlSerializableReader == null) { _xmlSerializableReader = new XmlSerializableReader(); } return(ReadIXmlSerializable(_xmlSerializableReader, xmlReader, xmlDataContract, isMemberType)); }
private void ExportXmlDataContract(XmlDataContract dataContract) { XmlQualifiedName typeQName; bool hasRoot; XmlSchemaType xsdType; Type clrType = dataContract.UnderlyingType; if (!IsSpecialXmlType(clrType, out typeQName, out xsdType, out hasRoot)) { if (!InvokeSchemaProviderMethod(clrType, _schemas, out typeQName, out xsdType, out hasRoot)) { InvokeGetSchemaMethod(clrType, _schemas, typeQName); } } if (hasRoot) { if (!(typeQName.Equals(dataContract.StableName))) { Fx.Assert("XML data contract type name does not match schema name"); } XmlSchema schema; if (SchemaHelper.GetSchemaElement(Schemas, new XmlQualifiedName(dataContract.TopLevelElementName.Value, dataContract.TopLevelElementNamespace.Value), out schema) == null) { XmlSchemaElement topLevelElement = ExportTopLevelElement(dataContract, schema); topLevelElement.IsNillable = dataContract.IsTopLevelElementNullable; ReprocessAll(_schemas); } XmlSchemaType anonymousType = xsdType; xsdType = SchemaHelper.GetSchemaType(_schemas, typeQName, out schema); if (anonymousType == null && xsdType == null && typeQName.Namespace != XmlSchema.Namespace) { throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataContractException(SR.Format(SR.MissingSchemaType, typeQName, DataContract.GetClrTypeFullName(clrType)))); } if (xsdType != null) { xsdType.Annotation = GetSchemaAnnotation( ExportSurrogateData(dataContract), dataContract.IsValueType ? GetAnnotationMarkup(IsValueTypeName, XmlConvert.ToString(dataContract.IsValueType), schema) : null ); } } }
public XmlQualifiedName GetSchemaTypeName(Type type) { if (type == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("type")); } type = this.GetSurrogatedType(type); DataContract dataContract = DataContract.GetDataContract(type); System.Runtime.Serialization.DataContractSet.EnsureTypeNotGeneric(dataContract.UnderlyingType); XmlDataContract contract2 = dataContract as XmlDataContract; if ((contract2 != null) && contract2.IsAnonymous) { return(XmlQualifiedName.Empty); } return(dataContract.StableName); }
public XmlSchemaType GetSchemaType(Type type) { if (type == null) { throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(nameof(type))); } type = GetSurrogatedType(type); DataContract dataContract = DataContract.GetDataContract(type); DataContractSet.EnsureTypeNotGeneric(dataContract.UnderlyingType); XmlDataContract xmlDataContract = dataContract as XmlDataContract; if (xmlDataContract != null && xmlDataContract.IsAnonymous) { return(xmlDataContract.XsdType); } return(null); }
private void SetElementType(XmlSchemaElement element, DataContract dataContract, XmlSchema schema) { XmlDataContract xmlDataContract = dataContract as XmlDataContract; if (xmlDataContract != null && xmlDataContract.IsAnonymous) { element.SchemaType = xmlDataContract.XsdType; } else { element.SchemaTypeName = dataContract.StableName; if (element.SchemaTypeName.Namespace.Equals(Globals.SerializationNamespace)) { schema.Namespaces.Add(Globals.SerPrefixForSchema, Globals.SerializationNamespace); } SchemaHelper.AddSchemaImport(dataContract.StableName.Namespace, schema); } }
internal override bool Equals(object other, Dictionary <DataContractPairKey, object> checkedContracts) { if (base.IsEqualOrChecked(other, checkedContracts)) { return(true); } XmlDataContract contract = other as XmlDataContract; if (contract == null) { return(false); } if (this.HasRoot != contract.HasRoot) { return(false); } if (this.IsAnonymous) { return(contract.IsAnonymous); } return((base.StableName.Name == contract.StableName.Name) && (base.StableName.Namespace == contract.StableName.Namespace)); }
void AddXmlDataContract(XmlDataContract xmlDataContract) { AddKnownDataContracts(xmlDataContract.KnownDataContracts); }
public static bool TryCreateBuiltInDataContract(string name, string ns, out DataContract dataContract) { dataContract = null; if (ns == DictionaryGlobals.SchemaNamespace.Value) { if (DictionaryGlobals.BooleanLocalName.Value == name) { dataContract = new BooleanDataContract(); } else if (DictionaryGlobals.SignedByteLocalName.Value == name) { dataContract = new SignedByteDataContract(); } else if (DictionaryGlobals.UnsignedByteLocalName.Value == name) { dataContract = new UnsignedByteDataContract(); } else if (DictionaryGlobals.ShortLocalName.Value == name) { dataContract = new ShortDataContract(); } else if (DictionaryGlobals.UnsignedShortLocalName.Value == name) { dataContract = new UnsignedShortDataContract(); } else if (DictionaryGlobals.IntLocalName.Value == name) { dataContract = new IntDataContract(); } else if (DictionaryGlobals.UnsignedIntLocalName.Value == name) { dataContract = new UnsignedIntDataContract(); } else if (DictionaryGlobals.LongLocalName.Value == name) { dataContract = new LongDataContract(); } else if (DictionaryGlobals.integerLocalName.Value == name) { dataContract = new IntegerDataContract(); } else if (DictionaryGlobals.positiveIntegerLocalName.Value == name) { dataContract = new PositiveIntegerDataContract(); } else if (DictionaryGlobals.negativeIntegerLocalName.Value == name) { dataContract = new NegativeIntegerDataContract(); } else if (DictionaryGlobals.nonPositiveIntegerLocalName.Value == name) { dataContract = new NonPositiveIntegerDataContract(); } else if (DictionaryGlobals.nonNegativeIntegerLocalName.Value == name) { dataContract = new NonNegativeIntegerDataContract(); } else if (DictionaryGlobals.UnsignedLongLocalName.Value == name) { dataContract = new UnsignedLongDataContract(); } else if (DictionaryGlobals.FloatLocalName.Value == name) { dataContract = new FloatDataContract(); } else if (DictionaryGlobals.DoubleLocalName.Value == name) { dataContract = new DoubleDataContract(); } else if (DictionaryGlobals.DecimalLocalName.Value == name) { dataContract = new DecimalDataContract(); } else if (DictionaryGlobals.DateTimeLocalName.Value == name) { dataContract = new DateTimeDataContract(); } else if (DictionaryGlobals.StringLocalName.Value == name) { dataContract = new StringDataContract(); } else if (DictionaryGlobals.timeLocalName.Value == name) { dataContract = new TimeDataContract(); } else if (DictionaryGlobals.dateLocalName.Value == name) { dataContract = new DateDataContract(); } else if (DictionaryGlobals.hexBinaryLocalName.Value == name) { dataContract = new HexBinaryDataContract(); } else if (DictionaryGlobals.gYearMonthLocalName.Value == name) { dataContract = new GYearMonthDataContract(); } else if (DictionaryGlobals.gYearLocalName.Value == name) { dataContract = new GYearDataContract(); } else if (DictionaryGlobals.gMonthDayLocalName.Value == name) { dataContract = new GMonthDayDataContract(); } else if (DictionaryGlobals.gDayLocalName.Value == name) { dataContract = new GDayDataContract(); } else if (DictionaryGlobals.gMonthLocalName.Value == name) { dataContract = new GMonthDataContract(); } else if (DictionaryGlobals.normalizedStringLocalName.Value == name) { dataContract = new NormalizedStringDataContract(); } else if (DictionaryGlobals.tokenLocalName.Value == name) { dataContract = new TokenDataContract(); } else if (DictionaryGlobals.languageLocalName.Value == name) { dataContract = new LanguageDataContract(); } else if (DictionaryGlobals.NameLocalName.Value == name) { dataContract = new NameDataContract(); } else if (DictionaryGlobals.NCNameLocalName.Value == name) { dataContract = new NCNameDataContract(); } else if (DictionaryGlobals.XSDIDLocalName.Value == name) { dataContract = new IDDataContract(); } else if (DictionaryGlobals.IDREFLocalName.Value == name) { dataContract = new IDREFDataContract(); } else if (DictionaryGlobals.IDREFSLocalName.Value == name) { dataContract = new IDREFSDataContract(); } else if (DictionaryGlobals.ENTITYLocalName.Value == name) { dataContract = new ENTITYDataContract(); } else if (DictionaryGlobals.ENTITIESLocalName.Value == name) { dataContract = new ENTITIESDataContract(); } else if (DictionaryGlobals.NMTOKENLocalName.Value == name) { dataContract = new NMTOKENDataContract(); } else if (DictionaryGlobals.NMTOKENSLocalName.Value == name) { dataContract = new NMTOKENDataContract(); } else if (DictionaryGlobals.ByteArrayLocalName.Value == name) { dataContract = new ByteArrayDataContract(); } else if (DictionaryGlobals.ObjectLocalName.Value == name) { dataContract = new ObjectDataContract(); } else if (DictionaryGlobals.TimeSpanLocalName.Value == name) { dataContract = new XsDurationDataContract(); } else if (DictionaryGlobals.UriLocalName.Value == name) { dataContract = new UriDataContract(); } else if (DictionaryGlobals.QNameLocalName.Value == name) { dataContract = new QNameDataContract(); } } else if (ns == DictionaryGlobals.SerializationNamespace.Value) { if (DictionaryGlobals.TimeSpanLocalName.Value == name) { dataContract = new TimeSpanDataContract(); } else if (DictionaryGlobals.GuidLocalName.Value == name) { dataContract = new GuidDataContract(); } else if (DictionaryGlobals.CharLocalName.Value == name) { dataContract = new CharDataContract(); } else if ("ArrayOfanyType" == name) { dataContract = new CollectionDataContract(typeof(Array)); } } else if (ns == DictionaryGlobals.AsmxTypesNamespace.Value) { if (DictionaryGlobals.CharLocalName.Value == name) { dataContract = new AsmxCharDataContract(); } else if (DictionaryGlobals.GuidLocalName.Value == name) { dataContract = new AsmxGuidDataContract(); } } else if (ns == "http://schemas.datacontract.org/2004/07/System.Xml") { if (name == "XmlElement") { dataContract = new XmlDataContract(typeof(XmlElement)); } else if (name == "ArrayOfXmlNode") { dataContract = new XmlDataContract(typeof(System.Xml.XmlNode[])); } } return (dataContract != null); }
public static bool TryCreateBuiltInDataContract(Type type, out DataContract dataContract) { if (type.IsEnum) { dataContract = null; return false; } dataContract = null; switch (Type.GetTypeCode(type)) { case TypeCode.Boolean: dataContract = new BooleanDataContract(); break; case TypeCode.Char: dataContract = new CharDataContract(); break; case TypeCode.SByte: dataContract = new SignedByteDataContract(); break; case TypeCode.Byte: dataContract = new UnsignedByteDataContract(); break; case TypeCode.Int16: dataContract = new ShortDataContract(); break; case TypeCode.UInt16: dataContract = new UnsignedShortDataContract(); break; case TypeCode.Int32: dataContract = new IntDataContract(); break; case TypeCode.UInt32: dataContract = new UnsignedIntDataContract(); break; case TypeCode.Int64: dataContract = new LongDataContract(); break; case TypeCode.UInt64: dataContract = new UnsignedLongDataContract(); break; case TypeCode.Single: dataContract = new FloatDataContract(); break; case TypeCode.Double: dataContract = new DoubleDataContract(); break; case TypeCode.Decimal: dataContract = new DecimalDataContract(); break; case TypeCode.DateTime: dataContract = new DateTimeDataContract(); break; case TypeCode.String: dataContract = new StringDataContract(); break; default: if (type == typeof(byte[])) { dataContract = new ByteArrayDataContract(); } else if (type == typeof(object)) { dataContract = new ObjectDataContract(); } else if (type == typeof(Uri)) { dataContract = new UriDataContract(); } else if (type == typeof(XmlQualifiedName)) { dataContract = new QNameDataContract(); } else if (type == typeof(TimeSpan)) { dataContract = new TimeSpanDataContract(); } else if (type == typeof(Guid)) { dataContract = new GuidDataContract(); } else if ((type == typeof(Enum)) || (type == typeof(ValueType))) { dataContract = new SpecialTypeDataContract(type, DictionaryGlobals.ObjectLocalName, DictionaryGlobals.SchemaNamespace); } else if (type == typeof(Array)) { dataContract = new CollectionDataContract(type); } else if ((type == typeof(XmlElement)) || (type == typeof(System.Xml.XmlNode[]))) { dataContract = new XmlDataContract(type); } break; } return (dataContract != null); }
private void AddXmlDataContract(XmlDataContract xmlDataContract) { this.AddKnownDataContracts(xmlDataContract.KnownDataContracts); }
internal static object ReadRootIXmlSerializable(XmlReaderDelegator xmlReader, XmlDataContract xmlDataContract, bool isMemberType) { return(ReadIXmlSerializable(new XmlSerializableReader(), xmlReader, xmlDataContract, isMemberType)); }
private void ExportXmlDataContract(XmlDataContract xmlDataContract, ContractCodeDomInfo contractCodeDomInfo) { GenerateType(xmlDataContract, contractCodeDomInfo); if (contractCodeDomInfo.ReferencedTypeExists) return; CodeTypeDeclaration type = contractCodeDomInfo.TypeDeclaration; if (SupportsPartialTypes) type.IsPartial = true; if (xmlDataContract.IsValueType) type.IsStruct = true; else { type.IsClass = true; type.BaseTypes.Add(Globals.TypeOfObject); } AddSerializableAttribute(GenerateSerializableTypes, type, contractCodeDomInfo); type.BaseTypes.Add(DataContract.GetClrTypeFullName(Globals.TypeOfIXmlSerializable)); type.Members.Add(NodeArrayField); type.Members.Add(NodeArrayProperty); type.Members.Add(ReadXmlMethod); type.Members.Add(WriteXmlMethod); type.Members.Add(GetSchemaMethod); if (xmlDataContract.IsAnonymous && !xmlDataContract.HasRoot) { type.CustomAttributes.Add(new CodeAttributeDeclaration( DataContract.GetClrTypeFullName(Globals.TypeOfXmlSchemaProviderAttribute), new CodeAttributeArgument(NullReference), new CodeAttributeArgument(Globals.IsAnyProperty, new CodePrimitiveExpression(true))) ); } else { type.CustomAttributes.Add(new CodeAttributeDeclaration( DataContract.GetClrTypeFullName(Globals.TypeOfXmlSchemaProviderAttribute), new CodeAttributeArgument(new CodePrimitiveExpression(Globals.ExportSchemaMethod))) ); CodeMemberField typeNameField = new CodeMemberField(Globals.TypeOfXmlQualifiedName, typeNameFieldName); typeNameField.Attributes |= MemberAttributes.Static | MemberAttributes.Private; XmlQualifiedName typeName = xmlDataContract.IsAnonymous ? SchemaImporter.ImportActualType(xmlDataContract.XsdType.Annotation, xmlDataContract.StableName, xmlDataContract.StableName) : xmlDataContract.StableName; typeNameField.InitExpression = new CodeObjectCreateExpression(Globals.TypeOfXmlQualifiedName, new CodePrimitiveExpression(typeName.Name), new CodePrimitiveExpression(typeName.Namespace)); type.Members.Add(typeNameField); type.Members.Add(GetSchemaStaticMethod); bool isElementNameDifferent = (xmlDataContract.TopLevelElementName != null && xmlDataContract.TopLevelElementName.Value != xmlDataContract.StableName.Name) || (xmlDataContract.TopLevelElementNamespace != null && xmlDataContract.TopLevelElementNamespace.Value != xmlDataContract.StableName.Namespace); if (isElementNameDifferent || xmlDataContract.IsTopLevelElementNullable == false) { CodeAttributeDeclaration xmlRootAttribute = new CodeAttributeDeclaration(DataContract.GetClrTypeFullName(Globals.TypeOfXmlRootAttribute)); if (isElementNameDifferent) { if (xmlDataContract.TopLevelElementName != null) { xmlRootAttribute.Arguments.Add(new CodeAttributeArgument("ElementName", new CodePrimitiveExpression(xmlDataContract.TopLevelElementName.Value))); } if (xmlDataContract.TopLevelElementNamespace != null) { xmlRootAttribute.Arguments.Add(new CodeAttributeArgument("Namespace", new CodePrimitiveExpression(xmlDataContract.TopLevelElementNamespace.Value))); } } if (xmlDataContract.IsTopLevelElementNullable == false) xmlRootAttribute.Arguments.Add(new CodeAttributeArgument("IsNullable", new CodePrimitiveExpression(false))); type.CustomAttributes.Add(xmlRootAttribute); } } AddPropertyChangedNotifier(contractCodeDomInfo, type.IsStruct); }
void ExportXmlDataContract(XmlDataContract dataContract) { XmlQualifiedName typeQName; bool hasRoot; XmlSchemaType xsdType; Type clrType = dataContract.UnderlyingType; if (!IsSpecialXmlType(clrType, out typeQName, out xsdType, out hasRoot)) if (!InvokeSchemaProviderMethod(clrType, schemas, out typeQName, out xsdType, out hasRoot)) InvokeGetSchemaMethod(clrType, schemas, typeQName); if (hasRoot) { if (!(typeQName.Equals(dataContract.StableName))) { Fx.Assert("XML data contract type name does not match schema name"); } XmlSchema schema; if (SchemaHelper.GetSchemaElement(Schemas, new XmlQualifiedName(dataContract.TopLevelElementName.Value, dataContract.TopLevelElementNamespace.Value), out schema) == null) { XmlSchemaElement topLevelElement = ExportTopLevelElement(dataContract, schema); topLevelElement.IsNillable = dataContract.IsTopLevelElementNullable; ReprocessAll(schemas); } XmlSchemaType anonymousType = xsdType; xsdType = SchemaHelper.GetSchemaType(schemas, typeQName, out schema); if (anonymousType == null && xsdType == null && typeQName.Namespace != XmlSchema.Namespace) { throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataContractException(SR.GetString(SR.MissingSchemaType, typeQName, DataContract.GetClrTypeFullName(clrType)))); } if (xsdType != null) xsdType.Annotation = GetSchemaAnnotation( ExportSurrogateData(dataContract), dataContract.IsValueType ? GetAnnotationMarkup(IsValueTypeName, XmlConvert.ToString(dataContract.IsValueType), schema) : null ); else if (DiagnosticUtility.ShouldTraceVerbose) { TraceUtility.Trace(TraceEventType.Verbose, TraceCode.XsdExportAnnotationFailed, SR.GetString(SR.TraceCodeXsdExportAnnotationFailed), new StringTraceRecord("Type", typeQName.Namespace + ":" + typeQName.Name)); } } }
private XmlDataContract ImportSpecialXmlDataType(XmlSchemaType xsdType, bool isAnonymous) { if (!isAnonymous) return null; XmlSchemaComplexType complexType = xsdType as XmlSchemaComplexType; if (complexType == null) return null; if (IsXmlAnyElementType(complexType)) { //check if the type is XElement XmlQualifiedName xlinqTypeName = new XmlQualifiedName("XElement", "http://schemas.datacontract.org/2004/07/System.Xml.Linq"); Type referencedType; if (dataContractSet.TryGetReferencedType(xlinqTypeName, null, out referencedType) && Globals.TypeOfIXmlSerializable.IsAssignableFrom(referencedType)) { XmlDataContract xmlDataContract = new XmlDataContract(referencedType); AddDataContract(xmlDataContract); return xmlDataContract; } //otherwise, assume XmlElement return (XmlDataContract)DataContract.GetBuiltInDataContract(Globals.TypeOfXmlElement); } if (IsXmlAnyType(complexType)) return (XmlDataContract)DataContract.GetBuiltInDataContract(Globals.TypeOfXmlNodeArray); return null; }
DataContract ImportXmlDataType(XmlQualifiedName typeName, XmlSchemaType xsdType, bool isAnonymous) { DataContract dataContract = dataContractSet[typeName]; if (dataContract != null) return dataContract; XmlDataContract xmlDataContract = ImportSpecialXmlDataType(xsdType, isAnonymous); if (xmlDataContract != null) return xmlDataContract; xmlDataContract = new XmlDataContract(); xmlDataContract.StableName = typeName; xmlDataContract.IsValueType = false; AddDataContract(xmlDataContract); if (xsdType != null) { ImportDataContractExtension(xsdType, xmlDataContract); xmlDataContract.IsValueType = IsValueType(typeName, xsdType.Annotation); xmlDataContract.IsTypeDefinedOnImport = true; xmlDataContract.XsdType = isAnonymous ? xsdType : null; xmlDataContract.HasRoot = !IsXmlAnyElementType(xsdType as XmlSchemaComplexType); } else { //Value type can be used by both nillable and non-nillable elements but reference type cannot be used by non nillable elements xmlDataContract.IsValueType = true; xmlDataContract.IsTypeDefinedOnImport = false; xmlDataContract.HasRoot = true; if (DiagnosticUtility.ShouldTraceVerbose) { TraceUtility.Trace(TraceEventType.Verbose, TraceCode.XsdImportAnnotationFailed, SR.GetString(SR.TraceCodeXsdImportAnnotationFailed), new StringTraceRecord("Type", typeName.Namespace + ":" + typeName.Name)); } } if (!isAnonymous) { bool isNullable; xmlDataContract.SetTopLevelElementName(SchemaHelper.GetGlobalElementDeclaration(schemaSet, typeName, out isNullable)); xmlDataContract.IsTopLevelElementNullable = isNullable; } return xmlDataContract; }
internal static object ReadIXmlSerializable(XmlSerializableReader xmlSerializableReader, XmlReaderDelegator xmlReader, XmlDataContract xmlDataContract, bool isMemberType) { object obj = null; xmlSerializableReader.BeginRead(xmlReader); if (isMemberType && !xmlDataContract.HasRoot) { xmlReader.Read(); xmlReader.MoveToContent(); } { IXmlSerializable xmlSerializable = xmlDataContract.CreateXmlSerializableDelegate(); xmlSerializable.ReadXml(xmlSerializableReader); obj = xmlSerializable; } xmlSerializableReader.EndRead(); return(obj); }
public XmlDataContractInterpreter (XmlDataContract contract) { this.contract = contract; }
public JsonXmlDataContract(XmlDataContract traditionalXmlDataContract) : base(traditionalXmlDataContract) { }
private DataContract ImportXmlDataType(XmlQualifiedName typeName, XmlSchemaType xsdType, bool isAnonymous) { DataContract contract = this.dataContractSet[typeName]; if (contract != null) { return contract; } XmlDataContract dataContract = this.ImportSpecialXmlDataType(xsdType, isAnonymous); if (dataContract == null) { dataContract = new XmlDataContract { StableName = typeName, IsValueType = false }; this.AddDataContract(dataContract); if (xsdType != null) { this.ImportDataContractExtension(xsdType, dataContract); dataContract.IsValueType = this.IsValueType(typeName, xsdType.Annotation); dataContract.IsTypeDefinedOnImport = true; dataContract.XsdType = isAnonymous ? xsdType : null; dataContract.HasRoot = !this.IsXmlAnyElementType(xsdType as XmlSchemaComplexType); } else { dataContract.IsValueType = true; dataContract.IsTypeDefinedOnImport = false; dataContract.HasRoot = true; if (DiagnosticUtility.ShouldTraceVerbose) { TraceUtility.Trace(TraceEventType.Verbose, 0x3000f, System.Runtime.Serialization.SR.GetString("TraceCodeXsdImportAnnotationFailed"), new StringTraceRecord("Type", typeName.Namespace + ":" + typeName.Name)); } } if (!isAnonymous) { bool flag; dataContract.SetTopLevelElementName(SchemaHelper.GetGlobalElementDeclaration(this.schemaSet, typeName, out flag)); dataContract.IsTopLevelElementNullable = flag; } } return dataContract; }
private XmlDataContract ImportSpecialXmlDataType(XmlSchemaType xsdType, bool isAnonymous) { if (isAnonymous) { XmlSchemaComplexType type = xsdType as XmlSchemaComplexType; if (type == null) { return null; } if (this.IsXmlAnyElementType(type)) { Type type2; XmlQualifiedName stableName = new XmlQualifiedName("XElement", "http://schemas.datacontract.org/2004/07/System.Xml.Linq"); if (this.dataContractSet.TryGetReferencedType(stableName, null, out type2) && Globals.TypeOfIXmlSerializable.IsAssignableFrom(type2)) { XmlDataContract dataContract = new XmlDataContract(type2); this.AddDataContract(dataContract); return dataContract; } return (XmlDataContract) DataContract.GetBuiltInDataContract(Globals.TypeOfXmlElement); } if (this.IsXmlAnyType(type)) { return (XmlDataContract) DataContract.GetBuiltInDataContract(Globals.TypeOfXmlNodeArray); } } return null; }
internal static object ReadIXmlSerializable(XmlSerializableReader xmlSerializableReader, XmlReaderDelegator xmlReader, XmlDataContract xmlDataContract, bool isMemberType) { object obj = null; xmlSerializableReader.BeginRead(xmlReader); if (isMemberType && !xmlDataContract.HasRoot) { xmlReader.Read(); xmlReader.MoveToContent(); } if (xmlDataContract.UnderlyingType == Globals.TypeOfXmlElement) { if (!xmlReader.IsStartElement()) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreateUnexpectedStateException(XmlNodeType.Element, xmlReader)); } XmlDocument xmlDoc = new XmlDocument(); obj = (XmlElement)xmlDoc.ReadNode(xmlSerializableReader); } else if (xmlDataContract.UnderlyingType == Globals.TypeOfXmlNodeArray) { obj = XmlSerializableServices.ReadNodes(xmlSerializableReader); } else { IXmlSerializable xmlSerializable = xmlDataContract.CreateXmlSerializableDelegate(); xmlSerializable.ReadXml(xmlSerializableReader); obj = xmlSerializable; } xmlSerializableReader.EndRead(); return(obj); }
private static DataContract CreateDataContract(int id, RuntimeTypeHandle typeHandle, Type type) { lock (createDataContractLock) { DataContract dataContract = dataContractCache[id]; if (dataContract == null) { if (type == null) { type = Type.GetTypeFromHandle(typeHandle); } type = DataContract.UnwrapNullableType(type); type = GetDataContractAdapterType(type); dataContract = GetBuiltInDataContract(type); if (dataContract == null) { if (type.IsArray) { dataContract = new CollectionDataContract(type); } else if (type.IsEnum) { dataContract = new EnumDataContract(type); } else if (type.IsGenericParameter) { dataContract = new GenericParameterDataContract(type); } else if (Globals.TypeOfIXmlSerializable.IsAssignableFrom(type)) { dataContract = new XmlDataContract(type); } else { if (type.IsPointer) { type = Globals.TypeOfReflectionPointer; } if (!CollectionDataContract.TryCreate(type, out dataContract)) { if ((!type.IsSerializable && !type.IsDefined(Globals.TypeOfDataContractAttribute, false)) && !ClassDataContract.IsNonAttributedTypeValidForSerialization(type)) { ThrowInvalidDataContractException(System.Runtime.Serialization.SR.GetString("TypeNotSerializable", new object[] { type }), type); } dataContract = new ClassDataContract(type); } } } } dataContractCache[id] = dataContract; return dataContract; } }
private void ExportXmlDataContract(XmlDataContract xmlDataContract, ContractCodeDomInfo contractCodeDomInfo) { this.GenerateType(xmlDataContract, contractCodeDomInfo); if (!contractCodeDomInfo.ReferencedTypeExists) { CodeTypeDeclaration typeDeclaration = contractCodeDomInfo.TypeDeclaration; if (this.SupportsPartialTypes) { typeDeclaration.IsPartial = true; } if (xmlDataContract.IsValueType) { typeDeclaration.IsStruct = true; } else { typeDeclaration.IsClass = true; typeDeclaration.BaseTypes.Add(Globals.TypeOfObject); } this.AddSerializableAttribute(this.GenerateSerializableTypes, typeDeclaration, contractCodeDomInfo); typeDeclaration.BaseTypes.Add(DataContract.GetClrTypeFullName(Globals.TypeOfIXmlSerializable)); typeDeclaration.Members.Add(this.NodeArrayField); typeDeclaration.Members.Add(this.NodeArrayProperty); typeDeclaration.Members.Add(this.ReadXmlMethod); typeDeclaration.Members.Add(this.WriteXmlMethod); typeDeclaration.Members.Add(this.GetSchemaMethod); if (xmlDataContract.IsAnonymous && !xmlDataContract.HasRoot) { typeDeclaration.CustomAttributes.Add(new CodeAttributeDeclaration(DataContract.GetClrTypeFullName(Globals.TypeOfXmlSchemaProviderAttribute), new CodeAttributeArgument[] { new CodeAttributeArgument(this.NullReference), new CodeAttributeArgument("IsAny", new CodePrimitiveExpression(true)) })); } else { CodeMemberField field; typeDeclaration.CustomAttributes.Add(new CodeAttributeDeclaration(DataContract.GetClrTypeFullName(Globals.TypeOfXmlSchemaProviderAttribute), new CodeAttributeArgument[] { new CodeAttributeArgument(new CodePrimitiveExpression("ExportSchema")) })); field = new CodeMemberField(Globals.TypeOfXmlQualifiedName, typeNameFieldName) { Attributes = field.Attributes | (MemberAttributes.Private | MemberAttributes.Static) }; XmlQualifiedName name = xmlDataContract.IsAnonymous ? SchemaImporter.ImportActualType(xmlDataContract.XsdType.Annotation, xmlDataContract.StableName, xmlDataContract.StableName) : xmlDataContract.StableName; field.InitExpression = new CodeObjectCreateExpression(Globals.TypeOfXmlQualifiedName, new CodeExpression[] { new CodePrimitiveExpression(name.Name), new CodePrimitiveExpression(name.Namespace) }); typeDeclaration.Members.Add(field); typeDeclaration.Members.Add(this.GetSchemaStaticMethod); bool flag = ((xmlDataContract.TopLevelElementName != null) && (xmlDataContract.TopLevelElementName.Value != xmlDataContract.StableName.Name)) || ((xmlDataContract.TopLevelElementNamespace != null) && (xmlDataContract.TopLevelElementNamespace.Value != xmlDataContract.StableName.Namespace)); if (flag || !xmlDataContract.IsTopLevelElementNullable) { CodeAttributeDeclaration declaration2 = new CodeAttributeDeclaration(DataContract.GetClrTypeFullName(Globals.TypeOfXmlRootAttribute)); if (flag) { if (xmlDataContract.TopLevelElementName != null) { declaration2.Arguments.Add(new CodeAttributeArgument("ElementName", new CodePrimitiveExpression(xmlDataContract.TopLevelElementName.Value))); } if (xmlDataContract.TopLevelElementNamespace != null) { declaration2.Arguments.Add(new CodeAttributeArgument("Namespace", new CodePrimitiveExpression(xmlDataContract.TopLevelElementNamespace.Value))); } } if (!xmlDataContract.IsTopLevelElementNullable) { declaration2.Arguments.Add(new CodeAttributeArgument("IsNullable", new CodePrimitiveExpression(false))); } typeDeclaration.CustomAttributes.Add(declaration2); } } this.AddPropertyChangedNotifier(contractCodeDomInfo, typeDeclaration.IsStruct); } }
private void ExportXmlDataContract(XmlDataContract dataContract) { XmlQualifiedName name; bool flag; XmlSchemaType type; Type underlyingType = dataContract.UnderlyingType; if (!IsSpecialXmlType(underlyingType, out name, out type, out flag) && !InvokeSchemaProviderMethod(underlyingType, this.schemas, out name, out type, out flag)) { InvokeGetSchemaMethod(underlyingType, this.schemas, name); } if (flag) { XmlSchema schema; name.Equals(dataContract.StableName); if (SchemaHelper.GetSchemaElement(this.Schemas, new XmlQualifiedName(dataContract.TopLevelElementName.Value, dataContract.TopLevelElementNamespace.Value), out schema) == null) { this.ExportTopLevelElement(dataContract, schema).IsNillable = dataContract.IsTopLevelElementNullable; ReprocessAll(this.schemas); } XmlSchemaType type3 = type; type = SchemaHelper.GetSchemaType(this.schemas, name, out schema); if (((type3 == null) && (type == null)) && (name.Namespace != "http://www.w3.org/2001/XMLSchema")) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataContractException(System.Runtime.Serialization.SR.GetString("MissingSchemaType", new object[] { name, DataContract.GetClrTypeFullName(underlyingType) }))); } if (type != null) { type.Annotation = GetSchemaAnnotation(new System.Xml.XmlNode[] { this.ExportSurrogateData(dataContract), dataContract.IsValueType ? this.GetAnnotationMarkup(IsValueTypeName, XmlConvert.ToString(dataContract.IsValueType), schema) : null }); } else if (DiagnosticUtility.ShouldTraceVerbose) { TraceUtility.Trace(TraceEventType.Verbose, 0x3000e, System.Runtime.Serialization.SR.GetString("TraceCodeXsdExportAnnotationFailed"), new StringTraceRecord("Type", name.Namespace + ":" + name.Name)); } } }
private void AddXmlDataContract(XmlDataContract xmlDataContract) { AddKnownDataContracts(xmlDataContract.KnownDataContracts); }
public XmlDataContractInterpreter(XmlDataContract contract) { this.contract = contract; }