private static void CheckSchemaVersion(XDocument document) { // Get the metadata node and look for the schemaVersion attribute XElement metadata = GetMetadataElement(document); if (metadata != null) { // Yank this attribute since we don't want to have to put it in our xsd XAttribute schemaVersionAttribute = metadata.Attribute(SchemaVersionAttributeName); if (schemaVersionAttribute != null) { schemaVersionAttribute.Remove(); } // Get the package id from the metadata node string packageId = GetPackageId(metadata); // If the schema of the document doesn't match any of our known schemas if (!ManifestSchemaUtility.IsKnownSchema(document.Root.Name.Namespace.NamespaceName)) { throw new InvalidOperationException( String.Format(CultureInfo.CurrentCulture, NuGetResources.IncompatibleSchema, packageId, typeof(Manifest).Assembly.GetNameSafe().Version)); } } }
public void Save(Stream stream, bool validate, int minimumManifestVersion) { if (validate) { // Validate before saving Validate(this); } int version = Math.Max(minimumManifestVersion, ManifestVersionUtility.GetManifestVersion(Metadata)); string schemaNamespace = ManifestSchemaUtility.GetSchemaNamespace(version); // Define the namespaces to use when serializing var ns = new XmlSerializerNamespaces(); ns.Add("", schemaNamespace); // Need to force the namespace here again as the default in order to get the XML output clean var serializer = new XmlSerializer(typeof(Manifest), schemaNamespace); using (var xmlWriter = new XmlTextWriter(stream, Encoding.UTF8)) { xmlWriter.Indentation = 4; xmlWriter.Formatting = Formatting.Indented; serializer.Serialize(xmlWriter, this, ns); } }
private static void ValidateManifestSchema(XDocument document, string schemaNamespace) { CheckSchemaVersion(document); // Create the schema set var schemaSet = ManifestSchemaUtility.GetManifestSchemaSet(schemaNamespace); // Validate the document document.Validate(schemaSet, (sender, e) => { if (e.Severity == XmlSeverityType.Error) { // Throw an exception if there is a validation error throw new InvalidOperationException(e.Message); } }); }