aboutsummaryrefslogtreecommitdiff
path: root/src/wix/WixToolset.Core.TestPackage/XmlNodeExtensions.cs
diff options
context:
space:
mode:
Diffstat (limited to 'src/wix/WixToolset.Core.TestPackage/XmlNodeExtensions.cs')
-rw-r--r--src/wix/WixToolset.Core.TestPackage/XmlNodeExtensions.cs90
1 files changed, 90 insertions, 0 deletions
diff --git a/src/wix/WixToolset.Core.TestPackage/XmlNodeExtensions.cs b/src/wix/WixToolset.Core.TestPackage/XmlNodeExtensions.cs
new file mode 100644
index 00000000..f4966f74
--- /dev/null
+++ b/src/wix/WixToolset.Core.TestPackage/XmlNodeExtensions.cs
@@ -0,0 +1,90 @@
1// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.
2
3namespace WixToolset.Core.TestPackage
4{
5 using System.Collections.Generic;
6 using System.IO;
7 using System.Text.RegularExpressions;
8 using System.Xml;
9
10 /// <summary>
11 /// Utility class to help compare XML in tests using string comparisons by using single quotes and stripping all namespaces.
12 /// </summary>
13 public static class XmlNodeExtensions
14 {
15 /// <summary>
16 /// Returns the node's outer XML using single quotes and stripping all namespaces.
17 /// </summary>
18 /// <param name="node"></param>
19 /// <param name="ignoredAttributesByElementName">Attributes for which the value should be set to '*'.</param>
20 /// <returns></returns>
21 public static string GetTestXml(this XmlNode node, Dictionary<string, List<string>> ignoredAttributesByElementName = null)
22 {
23 return node.OuterXml.GetTestXml(ignoredAttributesByElementName);
24 }
25
26 /// <summary>
27 /// Returns the XML using single quotes and stripping all namespaces.
28 /// </summary>
29 /// <param name="xml"></param>
30 /// <param name="ignoredAttributesByElementName">Attributes for which the value should be set to '*'.</param>
31 /// <returns></returns>
32 public static string GetTestXml(this string xml, Dictionary<string, List<string>> ignoredAttributesByElementName = null)
33 {
34 string formattedXml;
35 using (var sw = new StringWriter())
36 using (var writer = new TestXmlWriter(sw))
37 {
38 var doc = new XmlDocument();
39 doc.LoadXml(xml);
40
41 if (ignoredAttributesByElementName != null)
42 {
43 HandleIgnoredAttributes(doc, ignoredAttributesByElementName);
44 }
45
46 doc.Save(writer);
47 formattedXml = sw.ToString();
48 }
49
50 return Regex.Replace(formattedXml, " xmlns(:[^=]+)?='[^']*'", "");
51 }
52
53 private static void HandleIgnoredAttributes(XmlNode node, Dictionary<string, List<string>> ignoredAttributesByElementName)
54 {
55 if (node.Attributes != null && ignoredAttributesByElementName.TryGetValue(node.LocalName, out var ignoredAttributes))
56 {
57 foreach (var ignoredAttribute in ignoredAttributes)
58 {
59 var attribute = node.Attributes[ignoredAttribute];
60 if (attribute != null)
61 {
62 attribute.Value = "*";
63 }
64 }
65 }
66
67 if (node.ChildNodes != null)
68 {
69 foreach (XmlNode childNode in node.ChildNodes)
70 {
71 HandleIgnoredAttributes(childNode, ignoredAttributesByElementName);
72 }
73 }
74 }
75
76 private class TestXmlWriter : XmlTextWriter
77 {
78 public TestXmlWriter(TextWriter w)
79 : base(w)
80 {
81 this.QuoteChar = '\'';
82 }
83
84 public override void WriteStartDocument()
85 {
86 //OmitXmlDeclaration
87 }
88 }
89 }
90}