From 9af13dee4178af06385e50526e02316fbf1116fa Mon Sep 17 00:00:00 2001 From: Sujit Nayak Date: Tue, 30 Apr 2024 10:51:44 -0700 Subject: [PATCH] Fix BootstrapperBuilder code to correctly return the list of products available in the ClickOnce's bootstrapper packages folder --- .../BootstrapperUtil/BootstrapperBuilder.cs | 77 +++++-------------- 1 file changed, 20 insertions(+), 57 deletions(-) diff --git a/src/Tasks/BootstrapperUtil/BootstrapperBuilder.cs b/src/Tasks/BootstrapperUtil/BootstrapperBuilder.cs index 76e17988b08..accb98d7438 100644 --- a/src/Tasks/BootstrapperUtil/BootstrapperBuilder.cs +++ b/src/Tasks/BootstrapperUtil/BootstrapperBuilder.cs @@ -829,81 +829,44 @@ private XmlDocument LoadAndValidateXmlDocument(string filePath, bool validateFil if (fileExists) { - var xmlTextReader = new XmlTextReader(filePath) { DtdProcessing = DtdProcessing.Ignore }; - - XmlReader xmlReader = xmlTextReader; - + XmlReaderSettings xmlReaderSettings = new XmlReaderSettings(); + xmlReaderSettings.DtdProcessing = DtdProcessing.Ignore; if (validate) { -#pragma warning disable 618 // Using XmlValidatingReader. TODO: We need to switch to using XmlReader.Create() with validation. - using var validatingReader = new XmlValidatingReader(xmlReader); -#pragma warning restore 618 - var xrSettings = new XmlReaderSettings { DtdProcessing = DtdProcessing.Ignore, CloseInput = true }; - FileStream fs = File.OpenRead(schemaPath); - using (XmlReader xr = XmlReader.Create(fs, xrSettings)) + xmlReaderSettings.ValidationType = ValidationType.Schema; + xmlReaderSettings.XmlResolver = null; + xmlReaderSettings.ValidationEventHandler += results.SchemaValidationEventHandler; ; + xmlReaderSettings.Schemas.Add(null, schemaPath); + } + + using (StreamReader streamReader = new StreamReader(filePath)) + { + using (XmlReader xmlReader = XmlReader.Create(streamReader, xmlReaderSettings, filePath)) { try { - // first, add our schema to the validating reader's collection of schemas - var xmlSchema = validatingReader.Schemas.Add(null, xr); - - // if our schema namespace gets out of sync, - // then all of our calls to SelectNodes and SelectSingleNode will fail - Debug.Assert((xmlSchema != null) && - string.Equals(schemaNamespace, xmlSchema.TargetNamespace, StringComparison.Ordinal), - System.IO.Path.GetFileName(schemaPath) + " and BootstrapperBuilder.vb have mismatched namespaces, so the BootstrapperBuilder will fail to work."); - - // if we're supposed to be validating, then hook up our handler - validatingReader.ValidationEventHandler += results.SchemaValidationEventHandler; - - // switch readers so the doc does the actual read over the validating - // reader so we get validation events as we load the document - xmlReader = validatingReader; + Debug.Assert(_document != null, "our document should have been created by now!"); + xmlDocument = new XmlDocument(_document.NameTable); + xmlDocument.Load(xmlReader); } catch (XmlException ex) { - Debug.Fail("Failed to load schema '" + schemaPath + "' due to the following exception:\r\n" + ex.Message); - validate = false; + Debug.Fail("Failed to load document '" + filePath + "' due to the following exception:\r\n" + ex.Message); + return null; } catch (System.Xml.Schema.XmlSchemaException ex) { - Debug.Fail("Failed to load schema '" + schemaPath + "' due to the following exception:\r\n" + ex.Message); - validate = false; + Debug.Fail("Failed to load document '" + filePath + "' due to the following exception:\r\n" + ex.Message); + return null; } } } - try - { - Debug.Assert(_document != null, "our document should have been created by now!"); - xmlDocument = new XmlDocument(_document.NameTable); - xmlDocument.Load(xmlReader); - } - catch (XmlException ex) - { - Debug.Fail("Failed to load document '" + filePath + "' due to the following exception:\r\n" + ex.Message); - return null; - } - catch (System.Xml.Schema.XmlSchemaException ex) - { - Debug.Fail("Failed to load document '" + filePath + "' due to the following exception:\r\n" + ex.Message); - return null; - } - finally - { - xmlReader.Close(); - } - // Note that the xml document's default namespace must match the schema namespace // or none of our SelectNodes/SelectSingleNode calls will succeed Debug.Assert(xmlDocument.DocumentElement != null && - string.Equals(xmlDocument.DocumentElement.NamespaceURI, schemaNamespace, StringComparison.Ordinal), - "'" + xmlDocument.DocumentElement.NamespaceURI + "' is not '" + schemaNamespace + "'..."); - - if ((xmlDocument.DocumentElement == null) || - (!string.Equals(xmlDocument.DocumentElement.NamespaceURI, schemaNamespace, StringComparison.Ordinal))) - { - } + string.Equals(xmlDocument.DocumentElement.NamespaceURI, schemaNamespace, StringComparison.Ordinal), + "'" + xmlDocument.DocumentElement.NamespaceURI + "' is not '" + schemaNamespace + "'..."); } }