-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathXmlReaderExt.cs
More file actions
169 lines (145 loc) · 7.08 KB
/
XmlReaderExt.cs
File metadata and controls
169 lines (145 loc) · 7.08 KB
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
167
168
169
using System;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Xml;
// ReSharper disable UnusedMember.Global
namespace AltaSoft.DomainPrimitives;
/// <summary>
/// Provides extension methods for XmlReader and XmlWriter to simplify reading and writing of certain data types.
/// </summary>
public static class XmlReaderExt
{
/// <summary>
/// Reads the content of the current XML element as a <typeparamref name="T"/> value.
/// </summary>
/// <typeparam name="T">The type of value to parse, which must implement <see cref="IParsable{TSelf}"/>.</typeparam>
/// <param name="reader">The <see cref="XmlReader"/> instance.</param>
/// <returns>
/// A <typeparamref name="T"/> value parsed from the current element's content.
/// </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T ReadElementContentAs<T>(this XmlReader reader) where T : IParsable<T>
{
return T.Parse(reader.ReadElementContentAsString(), CultureInfo.InvariantCulture);
}
/// <summary>
/// Reads the content of the current XML element as a <see cref="DateTime"/> value.
/// </summary>
/// <param name="reader">The <see cref="XmlReader"/> instance.</param>
/// <param name="serializationFormat">serialization format to be used</param>
/// <returns>
/// A <see cref="DateTime"/> value parsed from the current element's content.
/// </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static DateTime ReadElementContentAsDateTime(this XmlReader reader, string serializationFormat)
{
var str = reader.ReadElementContentAsString();
if (DateTime.TryParseExact(str, serializationFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out var result))
return result;
return DateTime.Parse(str, CultureInfo.InvariantCulture);
}
/// <summary>
/// Reads the content of the current XML element as a <see cref="TimeOnly"/> value.
/// </summary>
/// <param name="reader">The <see cref="XmlReader"/> instance.</param>
/// <param name="serializationFormat">serialization format to be used</param>
/// <returns>
/// A <see cref="TimeOnly"/> value parsed from the current element's content.
/// </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static TimeOnly ReadElementContentAsTimeOnly(this XmlReader reader, string serializationFormat)
{
var str = reader.ReadElementContentAsString();
if (TimeOnly.TryParseExact(str, serializationFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out var result))
return result;
return TimeOnly.Parse(str, CultureInfo.InvariantCulture);
}
/// <summary>
/// Reads the content of the current XML element as a <see cref="DateOnly"/> value.
/// </summary>
/// <param name="reader">The <see cref="XmlReader"/> instance.</param>
/// <returns>
/// A <see cref="DateOnly"/> value parsed from the current element's content.
/// </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static DateOnly ReadElementContentAsDateOnly(this XmlReader reader)
{
var str = reader.ReadElementContentAsString();
if (DateOnly.TryParse(str, CultureInfo.InvariantCulture, out var result))
return result;
return DateOnly.FromDateTime(DateTime.Parse(str, CultureInfo.InvariantCulture));
}
/// <summary>
/// Reads the content of the current XML element as a <see cref="TimeOnly"/> value.
/// </summary>
/// <param name="reader">The <see cref="XmlReader"/> instance.</param>
/// <returns>
/// A <see cref="TimeOnly"/> value parsed from the current element's content.
/// </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static TimeOnly ReadElementContentAsTimeOnly(this XmlReader reader)
{
var str = reader.ReadElementContentAsString();
if (TimeOnly.TryParse(str, CultureInfo.InvariantCulture, out var result))
return result;
var dt = DateTimeOffset.ParseExact(str, s_acceptedFormats, CultureInfo.InvariantCulture, DateTimeStyles.None);
return TimeOnly.FromTimeSpan(dt.TimeOfDay);
}
/// <summary>
/// Reads the content of the current XML element as a <see cref="DateOnly"/> value.
/// </summary>
/// <param name="reader">The <see cref="XmlReader"/> instance.</param>
/// <param name="serializationFormat">serialization format to be used</param>
/// <returns>
/// A <see cref="DateOnly"/> value parsed from the current element's content.
/// </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static DateOnly ReadElementContentAsDateOnly(this XmlReader reader, string serializationFormat)
{
var str = reader.ReadElementContentAsString();
if (DateOnly.TryParseExact(str, serializationFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out var result))
return result;
if (DateOnly.TryParse(str, CultureInfo.InvariantCulture, out result))
return result;
return DateOnly.FromDateTime(DateTime.Parse(str, CultureInfo.InvariantCulture));
}
/// <summary>
/// Reads the content of the current XML element as a <see cref="DateTimeOffset" /> value.
/// </summary>
/// <param name="reader">The <see cref="XmlReader"/> instance.</param>
/// <param name="serializationFormat">serialization format to be used</param>
/// <returns>
/// A <see cref="DateTimeOffset"/> value parsed from the current element's content.
/// </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static DateTimeOffset ReadElementContentAsDateTimeOffset(this XmlReader reader, string serializationFormat)
{
var str = reader.ReadElementContentAsString();
if (DateTimeOffset.TryParseExact(str, serializationFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out var result))
return result;
return DateTimeOffset.Parse(str, CultureInfo.InvariantCulture);
}
/// <summary>
/// Reads the content of the current XML element as a <see cref="TimeSpan" /> value.
/// </summary>
/// <param name="reader">The <see cref="XmlReader"/> instance.</param>
/// <param name="serializationFormat">serialization format to be used</param>
/// <returns>
/// A <see cref="TimeSpan"/> value parsed from the current element's content.
/// </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static TimeSpan ReadElementContentAsTimeSpan(this XmlReader reader, string serializationFormat)
{
var str = reader.ReadElementContentAsString();
if (TimeSpan.TryParseExact(str, serializationFormat, CultureInfo.InvariantCulture, TimeSpanStyles.None, out var result))
return result;
return TimeSpan.Parse(str, CultureInfo.InvariantCulture);
}
private static readonly string[] s_acceptedFormats =
[
"HH:mm:ss",
"HH:mm:sszzz", // 15:00:00+04:00
"HH:mm:ssz", // 15:00:00Z
"HH:mm:ss'+'", // 15:00:00+ (bare plus)
];
}