aboutsummaryrefslogtreecommitdiff
path: root/src/WixToolset.Core.TestPackage/XmlNodeExtensions.cs
diff options
context:
space:
mode:
Diffstat (limited to 'src/WixToolset.Core.TestPackage/XmlNodeExtensions.cs')
-rw-r--r--src/WixToolset.Core.TestPackage/XmlNodeExtensions.cs75
1 files changed, 75 insertions, 0 deletions
diff --git a/src/WixToolset.Core.TestPackage/XmlNodeExtensions.cs b/src/WixToolset.Core.TestPackage/XmlNodeExtensions.cs
new file mode 100644
index 00000000..a7f04508
--- /dev/null
+++ b/src/WixToolset.Core.TestPackage/XmlNodeExtensions.cs
@@ -0,0 +1,75 @@
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 public static class XmlNodeExtensions
11 {
12 public static string GetTestXml(this XmlNode node, Dictionary<string, List<string>> ignoredAttributesByElementName = null)
13 {
14 return node.OuterXml.GetTestXml(ignoredAttributesByElementName);
15 }
16
17 public static string GetTestXml(this string xml, Dictionary<string, List<string>> ignoredAttributesByElementName = null)
18 {
19 string formattedXml;
20 using (var sw = new StringWriter())
21 using (var writer = new TestXmlWriter(sw))
22 {
23 var doc = new XmlDocument();
24 doc.LoadXml(xml);
25
26 if (ignoredAttributesByElementName != null)
27 {
28 HandleIgnoredAttributes(doc, ignoredAttributesByElementName);
29 }
30
31 doc.Save(writer);
32 formattedXml = sw.ToString();
33 }
34
35 return Regex.Replace(formattedXml, " xmlns(:[^=]+)?='[^']*'", "");
36 }
37
38 private static void HandleIgnoredAttributes(XmlNode node, Dictionary<string, List<string>> ignoredAttributesByElementName)
39 {
40 if (node.Attributes != null && ignoredAttributesByElementName.TryGetValue(node.LocalName, out var ignoredAttributes))
41 {
42 foreach (var ignoredAttribute in ignoredAttributes)
43 {
44 var attribute = node.Attributes[ignoredAttribute];
45 if (attribute != null)
46 {
47 attribute.Value = "*";
48 }
49 }
50 }
51
52 if (node.ChildNodes != null)
53 {
54 foreach (XmlNode childNode in node.ChildNodes)
55 {
56 HandleIgnoredAttributes(childNode, ignoredAttributesByElementName);
57 }
58 }
59 }
60
61 private class TestXmlWriter : XmlTextWriter
62 {
63 public TestXmlWriter(TextWriter w)
64 : base(w)
65 {
66 this.QuoteChar = '\'';
67 }
68
69 public override void WriteStartDocument()
70 {
71 //OmitXmlDeclaration
72 }
73 }
74 }
75}