/
XmlHelper.cs
166 lines (149 loc) · 6.46 KB
/
XmlHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// ***********************************************************************
// Copyright (c) 2010-2021 Charlie Poole, Terje Sandstrom
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// ***********************************************************************
using System;
using System.Globalization;
using System.Xml;
namespace NUnit.VisualStudio.TestAdapter
{
/// <summary>
/// XmlHelper provides static methods for basic XML operations.
/// </summary>
public static class XmlHelper
{
/// <summary>
/// Creates a new top level element node.
/// </summary>
/// <param name="name">The element name.</param>
/// <returns>A new XmlNode.</returns>
public static XmlNode CreateTopLevelElement(string name)
{
var doc = new XmlDocument();
doc.LoadXml("<" + name + "/>");
return doc.FirstChild;
}
public static XmlNode CreateXmlNode(string xml)
{
var doc = new XmlDocument();
doc.LoadXml(xml);
return doc.FirstChild;
}
public static XmlNode ToXml(this string xml)
{
var doc = new XmlDocument();
var fragment = doc.CreateDocumentFragment();
fragment.InnerXml = xml;
doc.AppendChild(fragment);
return doc.FirstChild;
}
/// <summary>
/// Adds an attribute with a specified name and value to an existing XmlNode.
/// </summary>
/// <param name="node">The node to which the attribute should be added.</param>
/// <param name="name">The name of the attribute.</param>
/// <param name="value">The value of the attribute.</param>
public static void AddAttribute(this XmlNode node, string name, string value)
{
var attr = node.OwnerDocument.CreateAttribute(name);
attr.Value = value;
node.Attributes.Append(attr);
}
/// <summary>
/// Adds a new element as a child of an existing XmlNode and returns it.
/// </summary>
/// <param name="node">The node to which the element should be added.</param>
/// <param name="name">The element name.</param>
/// <returns>The newly created child element.</returns>
public static XmlNode AddElement(this XmlNode node, string name)
{
XmlNode childNode = node.OwnerDocument.CreateElement(name);
node.AppendChild(childNode);
return childNode;
}
/// <summary>
/// Adds the a new element as a child of an existing node and returns it.
/// A CDataSection is added to the new element using the data provided.
/// </summary>
/// <param name="node">The node to which the element should be added.</param>
/// <param name="name">The element name.</param>
/// <param name="data">The data for the CDataSection.</param>
public static XmlNode AddElementWithCDataSection(this XmlNode node, string name, string data)
{
var childNode = node.AddElement(name);
childNode.AppendChild(node.OwnerDocument.CreateCDataSection(data));
return childNode;
}
#region Safe Attribute Access
/// <summary>
/// Gets the value of the given attribute.
/// </summary>
/// <param name="result">The result.</param>
/// <param name="name">The name.</param>
public static string GetAttribute(this XmlNode result, string name)
{
var attr = result.Attributes?[name];
return attr?.Value;
}
/// <summary>
/// Gets the value of the given attribute as an int.
/// </summary>
/// <param name="result">The result.</param>
/// <param name="name">The name.</param>
/// <param name="defaultValue">The default value.</param>
public static int GetAttribute(this XmlNode result, string name, int defaultValue)
{
var attr = result.Attributes[name];
return attr == null
? defaultValue
: int.Parse(attr.Value, CultureInfo.InvariantCulture);
}
/// <summary>
/// Gets the value of the given attribute as a double.
/// </summary>
/// <param name="result">The result.</param>
/// <param name="name">The name.</param>
/// <param name="defaultValue">The default value.</param>
public static double GetAttribute(this XmlNode result, string name, double defaultValue)
{
var attr = result.Attributes[name];
return attr == null
? defaultValue
: double.Parse(attr.Value, CultureInfo.InvariantCulture);
}
/// <summary>
/// Gets the value of the given attribute as a DateTime.
/// </summary>
/// <param name="result">The result.</param>
/// <param name="name">The name.</param>
/// <param name="defaultValue">The default value.</param>
public static DateTime GetAttribute(this XmlNode result, string name, DateTime defaultValue)
{
string dateStr = GetAttribute(result, name);
if (dateStr == null)
return defaultValue;
return !DateTime.TryParse(dateStr, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal | DateTimeStyles.AllowWhiteSpaces, out var date)
? defaultValue
: date;
}
#endregion
}
}