This repository has been archived by the owner on Dec 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
SerializableErrorWrapper.cs
105 lines (91 loc) · 3.33 KB
/
SerializableErrorWrapper.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
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
namespace Microsoft.AspNetCore.Mvc.Formatters.Xml
{
/// <summary>
/// Wrapper class for <see cref="SerializableError"/> to enable it to be serialized by the xml formatters.
/// </summary>
[XmlRoot("Error")]
public sealed class SerializableErrorWrapper : IXmlSerializable, IUnwrappable
{
// Note: XmlSerializer requires to have default constructor
public SerializableErrorWrapper()
{
SerializableError = new SerializableError();
}
/// <summary>
/// Initializes a new instance of the <see cref="SerializableErrorWrapper"/> class.
/// </summary>
/// <param name="error">The <see cref="SerializableError"/> object that needs to be wrapped.</param>
public SerializableErrorWrapper(SerializableError error)
{
if (error == null)
{
throw new ArgumentNullException(nameof(error));
}
SerializableError = error;
}
/// <summary>
/// Gets the wrapped object which is serialized/deserialized into XML
/// representation.
/// </summary>
public SerializableError SerializableError { get; }
/// <inheritdoc />
public XmlSchema GetSchema()
{
return null;
}
/// <summary>
/// Generates a <see cref="SerializableError"/> object from its XML representation.
/// </summary>
/// <param name="reader">The <see cref="XmlReader"/> stream from which the object is deserialized.</param>
public void ReadXml(XmlReader reader)
{
if (reader.IsEmptyElement)
{
reader.Read();
return;
}
reader.ReadStartElement();
while (reader.NodeType != XmlNodeType.EndElement)
{
var key = XmlConvert.DecodeName(reader.LocalName);
var value = reader.ReadInnerXml();
SerializableError.Add(key, value);
reader.MoveToContent();
}
reader.ReadEndElement();
}
/// <summary>
/// Converts the wrapped <see cref="SerializableError"/> object into its XML representation.
/// </summary>
/// <param name="writer">The <see cref="XmlWriter"/> stream to which the object is serialized.</param>
public void WriteXml(XmlWriter writer)
{
foreach (var keyValuePair in SerializableError)
{
var key = keyValuePair.Key;
var value = keyValuePair.Value;
writer.WriteStartElement(XmlConvert.EncodeLocalName(key));
if (value != null)
{
writer.WriteValue(value);
}
writer.WriteEndElement();
}
}
/// <inheritdoc />
public object Unwrap(Type declaredType)
{
if (declaredType == null)
{
throw new ArgumentNullException(nameof(declaredType));
}
return SerializableError;
}
}
}