From acba90f005779fa03ee05c3c87148bb6141ba60e Mon Sep 17 00:00:00 2001 From: Rob Mensching Date: Tue, 23 Jun 2020 16:43:11 -0700 Subject: Do *not* require XML declaration --- src/WixToolset.Converters/Wix3Converter.cs | 48 ++++++++++++++++++++---------- 1 file changed, 32 insertions(+), 16 deletions(-) (limited to 'src/WixToolset.Converters') diff --git a/src/WixToolset.Converters/Wix3Converter.cs b/src/WixToolset.Converters/Wix3Converter.cs index 2d603c4f..8c7d2049 100644 --- a/src/WixToolset.Converters/Wix3Converter.cs +++ b/src/WixToolset.Converters/Wix3Converter.cs @@ -203,9 +203,9 @@ namespace WixToolset.Converters { try { - using (var writer = File.CreateText(this.SourceFile)) + using (var writer = XmlWriter.Create(this.SourceFile, new XmlWriterSettings { OmitXmlDeclaration = true })) { - document.Save(writer, SaveOptions.DisableFormatting | SaveOptions.OmitDuplicateNamespaces); + document.Save(writer); } } catch (UnauthorizedAccessException) @@ -228,26 +228,17 @@ namespace WixToolset.Converters var declaration = document.Declaration; - // Convert the declaration. + // Remove the declaration. if (null != declaration) { - if (!String.Equals("utf-8", declaration.Encoding, StringComparison.OrdinalIgnoreCase)) + if (this.OnError(ConverterTestType.DeclarationPresent, null, "This file contains an XML declaration on the first line.")) { - if (this.OnError(ConverterTestType.DeclarationEncodingWrong, document.Root, "The XML declaration encoding is not properly set to 'utf-8'.")) - { - declaration.Encoding = "utf-8"; - } - } - } - else // missing declaration - { - if (this.OnError(ConverterTestType.DeclarationMissing, null, "This file is missing an XML declaration on the first line.")) - { - document.Declaration = new XDeclaration("1.0", "utf-8", null); - document.Root.AddBeforeSelf(new XText(XDocumentNewLine.ToString())); + document.Declaration = null; } } + TrimLeadingText(document); + // Start converting the nodes at the top. this.ConvertNodes(document.Nodes(), 0); @@ -903,6 +894,26 @@ namespace WixToolset.Converters return !String.IsNullOrEmpty(value); } + private static bool IsTextNode(XNode node, out XText text) + { + text = null; + + if (node.NodeType == XmlNodeType.Text || node.NodeType == XmlNodeType.CDATA) + { + text = (XText)node; + } + + return text != null; + } + + private static void TrimLeadingText(XDocument document) + { + while (IsTextNode(document.Nodes().FirstOrDefault(), out var text)) + { + text.Remove(); + } + } + private static string TrimTextValue(XText text) { var value = text.Value; @@ -1052,6 +1063,11 @@ namespace WixToolset.Converters /// Explicit auto-GUID unnecessary. /// AutoGuidUnnecessary, + + /// + /// Displayed when the XML declaration is present in the source file. + /// + DeclarationPresent, } } } -- cgit v1.2.3-55-g6feb