Пример #1
0
		/// <summary>
		/// Public ReadRootElement() method that deserializes a root-level Model instance from XML.
		/// </summary>
		/// <param name="serializationContext">Serialization context.</param>
		/// <param name="element">In-memory Model instance that will get the deserialized data.</param>
		/// <param name="reader">XmlReader to read serialized data from.</param>
		/// <param name="schemaResolver">An ISchemaResolver that allows the serializer to do schema validation on the root element (and everything inside it).</param>
		public override void ReadRootElement(DslModeling::SerializationContext serializationContext, DslModeling::ModelElement element, global::System.Xml.XmlReader reader, DslModeling::ISchemaResolver schemaResolver)
		{
			#region Check Parameters
			global::System.Diagnostics.Debug.Assert(serializationContext != null);
			if (serializationContext == null)
				throw new global::System.ArgumentNullException("serializationContext");
			global::System.Diagnostics.Debug.Assert(reader != null);
			if (reader == null)
				throw new global::System.ArgumentNullException("reader");
			#endregion
	
			// Version check.
			CheckVersion(serializationContext, reader);
	
			if (!serializationContext.Result.Failed)
			{	// Check to see if schema validation is possible.
				if (schemaResolver != null)
				{
					string targetNamespace = reader.NamespaceURI;
					if (!string.IsNullOrEmpty(targetNamespace))
					{
						global::System.Collections.Generic.IList<string> schemas = schemaResolver.ResolveSchema(targetNamespace);
						if (schemas != null && schemas.Count > 0)
						{
							if (schemas.Count > 1)
							{
								ActiveWriterSerializationBehaviorSerializationMessages.AmbiguousSchema(serializationContext, reader, targetNamespace, schemas[0]);
							}
							global::System.Xml.Schema.XmlSchemaSet schemaSet = new global::System.Xml.Schema.XmlSchemaSet(reader.NameTable);
							schemaSet.Add(targetNamespace, schemas[0]);
							global::System.Xml.XmlReaderSettings readerSettings = new global::System.Xml.XmlReaderSettings();
							readerSettings.ConformanceLevel = global::System.Xml.ConformanceLevel.Auto;
							readerSettings.ValidationType = global::System.Xml.ValidationType.Schema;
							readerSettings.Schemas = schemaSet;
							ActiveWriterSerializationBehaviorSchemaValidationCallback validationCallback = new ActiveWriterSerializationBehaviorSchemaValidationCallback(serializationContext);
							readerSettings.ValidationEventHandler += new global::System.Xml.Schema.ValidationEventHandler(validationCallback.Handler);
							// Wrap the given reader as a validating reader and carry out the normal deserialization.
							using (global::System.Xml.XmlReader validatingReader = global::System.Xml.XmlReader.Create(reader, readerSettings))
							{
								validationCallback.Reader = validatingReader;
								this.Read(serializationContext, element, validatingReader);
							}
							return;
						}
						else
						{
							ActiveWriterSerializationBehaviorSerializationMessages.NoSchema(serializationContext, reader, targetNamespace);
						}
					}
				}
	
				// No schema information available, carry out the normal deserialization.
				this.Read(serializationContext, element, reader);
			}
		}
Пример #2
0
		/// <summary>
		/// Attempts to return a validating XML reader
		/// </summary>
		/// <returns>If all of the schema for registered serializers and all of the schema referenced in the file can be resolved,
		/// then a validating reader is returned. Otherwise, the supplied reader is returned.
		/// The serialization context will contain warning messages for all schema that could not be resolved.</returns>
		protected virtual global::System.Xml.XmlReader TryCreateValidatingReader(DslModeling::ISchemaResolver schemaResolver, global::System.Xml.XmlReader reader, DslModeling::SerializationContext serializationContext)
		{
			System.Diagnostics.Debug.Assert(serializationContext != null, "Serialization context should not be null");
	
			// Can't do anything without a schema resolver
			if (schemaResolver == null)
			{
				return reader;
			}
			
			global::System.Xml.Schema.XmlSchemaSet schemaSet = new global::System.Xml.Schema.XmlSchemaSet(reader.NameTable);
	
			// Combine the list of namespaces for models in the store with those referred to in the current node
			System.Collections.Generic.IEnumerable<string> namespaces = serializationContext.Directory.Namespaces.Select(n => n.TargetNamespace);
			namespaces = namespaces.Concat(DslModeling::SerializationUtilities.GetNamespacesFromCurrentNode(reader));		
	
			bool success = true;
			foreach (string ns in namespaces.Distinct())
			{
				// Try to resolve all namespaces so warnings are shown for all missing namespaces
				success = ResolveSchema(ns, schemaSet, schemaResolver, reader, serializationContext) & success;
			}
	
			if (success && schemaSet.Count > 0)
			{
				global::System.Xml.XmlReaderSettings readerSettings = reader.Settings.Clone();
				readerSettings.ConformanceLevel = global::System.Xml.ConformanceLevel.Auto;
				readerSettings.ValidationType = global::System.Xml.ValidationType.Schema;
				readerSettings.Schemas = schemaSet;
				ActiveWriterSerializationBehaviorSchemaValidationCallback validationCallback = new ActiveWriterSerializationBehaviorSchemaValidationCallback(serializationContext);
				readerSettings.ValidationEventHandler += new global::System.Xml.Schema.ValidationEventHandler(validationCallback.Handler);
	
	
				// Wrap the given reader as a validating reader and carry out the normal deserialization.
				global::System.Xml.XmlReader validatingReader = global::System.Xml.XmlReader.Create(reader, readerSettings);
	
				validationCallback.Reader = validatingReader;
				return validatingReader;
			}
	
			return reader;
		}