forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXmlSerializableWrapper.cs
More file actions
111 lines (98 loc) · 3.38 KB
/
XmlSerializableWrapper.cs
File metadata and controls
111 lines (98 loc) · 3.38 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
#if !(SL5 || __IOS__ || XBOX || ANDROID || PCL)
using System;
using System.Runtime.Serialization;
using System.Xml;
using System.Xml.Serialization;
using ServiceStack.Text;
namespace ServiceStack.Serialization
{
public sealed class XmlSerializerWrapper : XmlObjectSerializer
{
System.Xml.Serialization.XmlSerializer serializer;
string defaultNS;
readonly Type objectType;
public XmlSerializerWrapper(Type type)
: this(type, null, null)
{
}
public XmlSerializerWrapper(Type type, string name, string ns)
{
this.objectType = type;
if (!String.IsNullOrEmpty(ns))
{
this.defaultNS = ns;
this.serializer = new System.Xml.Serialization.XmlSerializer(type, ns);
}
else
{
this.defaultNS = GetNamespace(type);
this.serializer = new System.Xml.Serialization.XmlSerializer(type);
}
}
public override bool IsStartObject(XmlDictionaryReader reader)
{
throw new NotImplementedException();
}
public override object ReadObject(XmlDictionaryReader reader, bool verifyObjectName)
{
throw new NotImplementedException();
}
public override void WriteEndObject(XmlDictionaryWriter writer)
{
throw new NotImplementedException();
}
public override void WriteObjectContent(XmlDictionaryWriter writer, object graph)
{
throw new NotImplementedException();
}
public override void WriteStartObject(XmlDictionaryWriter writer, object graph)
{
throw new NotImplementedException();
}
public override void WriteObject(XmlDictionaryWriter writer, object graph)
{
this.serializer.Serialize(writer, graph);
}
public override object ReadObject(XmlDictionaryReader reader)
{
string readersNS;
readersNS = (String.IsNullOrEmpty(reader.NamespaceURI)) ? "" : reader.NamespaceURI;
if (String.Compare(this.defaultNS, readersNS) != 0)
{
this.serializer = new System.Xml.Serialization.XmlSerializer(this.objectType, readersNS);
this.defaultNS = readersNS;
}
return (this.serializer.Deserialize(reader));
}
/// <summary>
/// Gets the namespace from an attribute marked on the type's definition
/// </summary>
/// <param name="type"></param>
/// <returns>Namespace of type</returns>
public static string GetNamespace(Type type)
{
var dcAttr = type.FirstAttribute<DataContractAttribute>();
if (dcAttr != null)
{
return dcAttr.Namespace;
}
var xrAttr = type.FirstAttribute<XmlRootAttribute>();
if (xrAttr != null)
{
return xrAttr.Namespace;
}
var xtAttr = type.FirstAttribute<XmlTypeAttribute>();
if (xtAttr != null)
{
return xtAttr.Namespace;
}
var xeAttr = type.FirstAttribute<XmlElementAttribute>();
if (xeAttr != null)
{
return xeAttr.Namespace;
}
return null;
}
}
}
#endif