public override async Task <string> ReadContentAsStringAsync() { if (!CanReadContentAs(this.NodeType)) { throw CreateReadContentAsException(nameof(ReadContentAsString)); } object typedValue = await InternalReadContentAsObjectAsync().ConfigureAwait(false); XmlSchemaType?xmlType = NodeType == XmlNodeType.Attribute ? AttributeXmlType : ElementXmlType; try { if (xmlType != null) { return(xmlType.ValueConverter.ToString(typedValue)); } else { return((typedValue as string) !); } } catch (InvalidCastException e) { throw new XmlException(SR.Xml_ReadContentAsFormatException, "String", e, this as IXmlLineInfo); } catch (FormatException e) { throw new XmlException(SR.Xml_ReadContentAsFormatException, "String", e, this as IXmlLineInfo); } catch (OverflowException e) { throw new XmlException(SR.Xml_ReadContentAsFormatException, "String", e, this as IXmlLineInfo); } }
internal SchemaObjectInfo(XmlSchemaType?type, XmlSchemaElement?element, XmlSchema?schema, List <XmlSchemaType>?knownTypes) { _type = type; _element = element; _schema = schema; _knownTypes = knownTypes; }
internal XmlNameEx(string prefix, string localName, string ns, int hashCode, XmlDocument ownerDoc, XmlName next, IXmlSchemaInfo schemaInfo) : base(prefix, localName, ns, hashCode, ownerDoc, next) { SetValidity(schemaInfo.Validity); SetIsDefault(schemaInfo.IsDefault); SetIsNil(schemaInfo.IsNil); _memberType = schemaInfo.MemberType; _schemaType = schemaInfo.SchemaType; _decl = schemaInfo.SchemaElement != null ? (object)schemaInfo.SchemaElement : (object?)schemaInfo.SchemaAttribute; }
internal void Clear() { _isNil = false; _isDefault = false; _schemaType = null; _schemaElement = null; _schemaAttribute = null; _memberType = null; _validity = XmlSchemaValidity.NotKnown; _contentType = XmlSchemaContentType.Empty; }
public static bool IsDerivedFrom([NotNullWhen(true)] XmlSchemaType?derivedType, [NotNullWhen(true)] XmlSchemaType?baseType, XmlSchemaDerivationMethod except) { if (derivedType == null || baseType == null) { return(false); } if (derivedType == baseType) { return(true); } if (baseType == XmlSchemaComplexType.AnyType) { //Not checking for restriction blocked since all types are implicitly derived by restriction from xs:anyType return(true); } do { XmlSchemaSimpleType?dt = derivedType as XmlSchemaSimpleType; XmlSchemaSimpleType?bt = baseType as XmlSchemaSimpleType; if (bt != null && dt != null) { //SimpleTypes if (bt == DatatypeImplementation.AnySimpleType) { //Not checking block=restriction return(true); } if ((except & derivedType.DerivedBy) != 0 || !dt.Datatype !.IsDerivedFrom(bt.Datatype !)) { return(false); } return(true); } else { //Complex types if ((except & derivedType.DerivedBy) != 0) { return(false); } derivedType = derivedType.BaseXmlSchemaType; if (derivedType == baseType) { return(true); } } } while (derivedType != null); return(false); }
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 override async Task <object> ReadContentAsAsync(Type returnType, IXmlNamespaceResolver namespaceResolver) { if (!CanReadContentAs(this.NodeType)) { throw CreateReadContentAsException(nameof(ReadContentAs)); } string originalStringValue; var tuple_0 = await InternalReadContentAsObjectTupleAsync(false).ConfigureAwait(false); originalStringValue = tuple_0.Item1; object typedValue = tuple_0.Item2; XmlSchemaType?xmlType = NodeType == XmlNodeType.Attribute ? AttributeXmlType : ElementXmlType; try { if (xmlType != null) { // special-case convertions to DateTimeOffset; typedValue is by default a DateTime // which cannot preserve time zone, so we need to convert from the original string if (returnType == typeof(DateTimeOffset) && xmlType.Datatype is Datatype_dateTimeBase) { typedValue = originalStringValue; } return(xmlType.ValueConverter.ChangeType(typedValue, returnType)); } else { return(XmlUntypedConverter.Untyped.ChangeType(typedValue, returnType, namespaceResolver)); } } catch (FormatException e) { throw new XmlException(SR.Xml_ReadContentAsFormatException, returnType.ToString(), e, this as IXmlLineInfo); } catch (InvalidCastException e) { throw new XmlException(SR.Xml_ReadContentAsFormatException, returnType.ToString(), e, this as IXmlLineInfo); } catch (OverflowException e) { throw new XmlException(SR.Xml_ReadContentAsFormatException, returnType.ToString(), e, this as IXmlLineInfo); } }
public int GetHashCode(XmlSchemaInfo?si) { if (si == null) { return(0); } int h = (int)si.ContentType; if (si.IsDefault) { h ^= 1; } if (si.IsNil) { h ^= 1; } XmlSchemaSimpleType?memberType = si.MemberType; if (memberType != null) { h ^= memberType.GetHashCode(); } XmlSchemaAttribute?schemaAttribute = si.SchemaAttribute; if (schemaAttribute != null) { h ^= schemaAttribute.GetHashCode(); } XmlSchemaElement?schemaElement = si.SchemaElement; if (schemaElement != null) { h ^= schemaElement.GetHashCode(); } XmlSchemaType?schemaType = si.SchemaType; if (schemaType != null) { h ^= schemaType.GetHashCode(); } h ^= (int)si.Validity; return(h); }
internal static XmlSchemaType?GetSchemaType(XmlSchemaSet schemas, XmlQualifiedName typeQName, out XmlSchema?outSchema) { outSchema = null; ICollection currentSchemas = schemas.Schemas(); string ns = typeQName.Namespace; foreach (XmlSchema schema in currentSchemas) { if (NamespacesEqual(ns, schema.TargetNamespace)) { outSchema = schema; foreach (XmlSchemaObject schemaObj in schema.Items) { XmlSchemaType?schemaType = schemaObj as XmlSchemaType; if (schemaType != null && schemaType.Name == typeQName.Name) { return(schemaType); } } } } return(null); }
private async Task <Tuple <XmlSchemaType, string, object> > InternalReadElementContentAsObjectTupleAsync(bool unwrapTypedValue) { XmlSchemaType?xmlType = null; string originalString; Debug.Assert(this.NodeType == XmlNodeType.Element); object typedValue; // If its an empty element, can have default/fixed value if (this.IsEmptyElement) { if (_xmlSchemaInfo.ContentType == XmlSchemaContentType.TextOnly) { typedValue = ReturnBoxedValue(_atomicValue, _xmlSchemaInfo.XmlType, unwrapTypedValue) !; } else { typedValue = _atomicValue !; } Debug.Assert(_originalAtomicValueString != null); originalString = _originalAtomicValueString; xmlType = ElementXmlType; // Set this for default values await this.ReadAsync().ConfigureAwait(false); return(new Tuple <XmlSchemaType, string, object>(xmlType, originalString, typedValue)); } // move to content and read typed value await this.ReadAsync().ConfigureAwait(false); if (this.NodeType == XmlNodeType.EndElement) { // If IsDefault is true, the next node will be EndElement if (_xmlSchemaInfo.IsDefault) { if (_xmlSchemaInfo.ContentType == XmlSchemaContentType.TextOnly) { typedValue = ReturnBoxedValue(_atomicValue, _xmlSchemaInfo.XmlType, unwrapTypedValue) !; } else { // anyType has default value typedValue = _atomicValue !; } Debug.Assert(_originalAtomicValueString != null); originalString = _originalAtomicValueString; } else { // Empty content typedValue = string.Empty; originalString = string.Empty; } } else if (this.NodeType == XmlNodeType.Element) { // the first child is again element node throw new XmlException(SR.Xml_MixedReadElementContentAs, string.Empty, this as IXmlLineInfo); } else { var content = await InternalReadContentAsObjectTupleAsync(unwrapTypedValue).ConfigureAwait(false); originalString = content.Item1; typedValue = content.Item2; // ReadElementContentAsXXX cannot be called on mixed content, if positioned on node other than EndElement, Error if (this.NodeType != XmlNodeType.EndElement) { throw new XmlException(SR.Xml_MixedReadElementContentAs, string.Empty, this as IXmlLineInfo); } } xmlType = ElementXmlType; // Set this as we are moving ahead to the next node // move to next node await this.ReadAsync().ConfigureAwait(false); return(new Tuple <XmlSchemaType, string, object>(xmlType, originalString, typedValue)); }
internal abstract XmlSchemaDatatype DeriveByList(XmlSchemaType?schemaType);
internal static bool IsSpecialXmlType(Type type, [NotNullWhen(true)] out XmlQualifiedName?typeName, [NotNullWhen(true)] out XmlSchemaType?xsdType, out bool hasRoot) { xsdType = null; hasRoot = true; if (type == Globals.TypeOfXmlElement || type == Globals.TypeOfXmlNodeArray) { string?name = null; if (type == Globals.TypeOfXmlElement) { xsdType = CreateAnyElementType(); name = "XmlElement"; hasRoot = false; } else { xsdType = CreateAnyType(); name = "ArrayOfXmlNode"; hasRoot = true; } typeName = new XmlQualifiedName(name, DataContract.GetDefaultStableNamespace(type)); return(true); } typeName = null; return(false); }
private static bool InvokeSchemaProviderMethod(Type clrType, XmlSchemaSet schemas, out XmlQualifiedName stableName, out XmlSchemaType?xsdType, out bool hasRoot) { xsdType = null; hasRoot = true; object[] attrs = clrType.GetCustomAttributes(Globals.TypeOfXmlSchemaProviderAttribute, false); if (attrs == null || attrs.Length == 0) { stableName = DataContract.GetDefaultStableName(clrType); return(false); } XmlSchemaProviderAttribute provider = (XmlSchemaProviderAttribute)attrs[0]; if (provider.IsAny) { xsdType = CreateAnyElementType(); hasRoot = false; } string?methodName = provider.MethodName; if (methodName == null || methodName.Length == 0) { if (!provider.IsAny) { throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataContractException(SR.Format(SR.InvalidGetSchemaMethod, DataContract.GetClrTypeFullName(clrType)))); } stableName = DataContract.GetDefaultStableName(clrType); } else { MethodInfo?getMethod = clrType.GetMethod(methodName, /*BindingFlags.DeclaredOnly |*/ BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public, new Type[] { typeof(XmlSchemaSet) }); if (getMethod == null) { throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataContractException(SR.Format(SR.MissingGetSchemaMethod, DataContract.GetClrTypeFullName(clrType), methodName))); } if (!(Globals.TypeOfXmlQualifiedName.IsAssignableFrom(getMethod.ReturnType)) && !(Globals.TypeOfXmlSchemaType.IsAssignableFrom(getMethod.ReturnType))) { throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataContractException(SR.Format(SR.InvalidReturnTypeOnGetSchemaMethod, DataContract.GetClrTypeFullName(clrType), methodName, DataContract.GetClrTypeFullName(getMethod.ReturnType), DataContract.GetClrTypeFullName(Globals.TypeOfXmlQualifiedName), typeof(XmlSchemaType)))); } object?typeInfo = getMethod.Invoke(null, new object[] { schemas }); if (provider.IsAny) { if (typeInfo != null) { throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataContractException(SR.Format(SR.InvalidNonNullReturnValueByIsAny, DataContract.GetClrTypeFullName(clrType), methodName))); } stableName = DataContract.GetDefaultStableName(clrType); } else if (typeInfo == null) { xsdType = CreateAnyElementType(); hasRoot = false; stableName = DataContract.GetDefaultStableName(clrType); } else { XmlSchemaType?providerXsdType = typeInfo as XmlSchemaType; if (providerXsdType != null) { string?typeName = providerXsdType.Name; string?typeNs = null; if (typeName == null || typeName.Length == 0) { DataContract.GetDefaultStableName(DataContract.GetClrTypeFullName(clrType), out typeName, out typeNs); stableName = new XmlQualifiedName(typeName, typeNs); providerXsdType.Annotation = GetSchemaAnnotation(ExportActualType(stableName, new XmlDocument())); xsdType = providerXsdType; } else { foreach (XmlSchema schema in schemas.Schemas()) { foreach (XmlSchemaObject schemaItem in schema.Items) { if ((object)schemaItem == (object)providerXsdType) { typeNs = schema.TargetNamespace; if (typeNs == null) { typeNs = string.Empty; } break; } } if (typeNs != null) { break; } } if (typeNs == null) { throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataContractException(SR.Format(SR.MissingSchemaType, typeName, DataContract.GetClrTypeFullName(clrType)))); } stableName = new XmlQualifiedName(typeName, typeNs); } } else { stableName = (XmlQualifiedName)typeInfo; } } } return(true); }
internal static void GetXmlTypeInfo(Type type, out XmlQualifiedName stableName, out XmlSchemaType?xsdType, out bool hasRoot) { if (IsSpecialXmlType(type, out stableName !, out xsdType, out hasRoot)) { return; } XmlSchemaSet schemas = new XmlSchemaSet(); schemas.XmlResolver = null; InvokeSchemaProviderMethod(type, schemas, out stableName, out xsdType, out hasRoot); if (stableName.Name == null || stableName.Name.Length == 0) { throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataContractException(SR.Format(SR.InvalidXmlDataContractName, DataContract.GetClrTypeFullName(type)))); } }
internal void SetElementType(XmlSchemaType?value) { _elementType = value; }
internal void SetBaseSchemaType(XmlSchemaType?value) { _baseSchemaType = value; }