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
114 lines (101 loc) · 3.88 KB
/
XmlSerializableWrapper.cs
File metadata and controls
114 lines (101 loc) · 3.88 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
#if !SILVERLIGHT && !MONOTOUCH && !XBOX
using System;
using System.Runtime.Serialization;
using System.Xml;
using System.Xml.Serialization;
namespace ServiceStack.ServiceModel.Support
{
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)
{
Attribute[] attrs = (Attribute[])type.GetCustomAttributes(typeof(DataContractAttribute), true);
if (attrs.Length > 0)
{
DataContractAttribute dcAttr = (DataContractAttribute)attrs[0];
return dcAttr.Namespace;
}
attrs = (Attribute[])type.GetCustomAttributes(typeof(XmlRootAttribute), true);
if (attrs.Length > 0)
{
XmlRootAttribute xmlAttr = (XmlRootAttribute)attrs[0];
return xmlAttr.Namespace;
}
attrs = (Attribute[])type.GetCustomAttributes(typeof(XmlTypeAttribute), true);
if (attrs.Length > 0)
{
XmlTypeAttribute xmlAttr = (XmlTypeAttribute)attrs[0];
return xmlAttr.Namespace;
}
attrs = (Attribute[])type.GetCustomAttributes(typeof(XmlElementAttribute), true);
if (attrs.Length > 0)
{
XmlElementAttribute xmlAttr = (XmlElementAttribute)attrs[0];
return xmlAttr.Namespace;
}
return null;
}
}
}
#endif