Skip to content

Commit 3d883d1

Browse files
committed
adds the ability to specify whether the XmlDeclaration should be omitted in the xml output stream. Defaults to false (the XmlDeclaration should be included in the output)
1 parent bf25efe commit 3d883d1

2 files changed

Lines changed: 138 additions & 132 deletions

File tree

src/ServiceStack.Text.suo

0 Bytes
Binary file not shown.

src/ServiceStack.Text/XmlSerializer.cs

Lines changed: 138 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -10,137 +10,143 @@
1010
namespace ServiceStack.Text
1111
{
1212
#if !XBOX
13-
public class XmlSerializer
14-
{
15-
private readonly XmlDictionaryReaderQuotas quotas;
16-
17-
public static XmlSerializer Instance
18-
= new XmlSerializer(new XmlDictionaryReaderQuotas {
19-
MaxStringContentLength = 1024 * 1024,
20-
});
21-
22-
public XmlSerializer(XmlDictionaryReaderQuotas quotas)
23-
{
24-
this.quotas = quotas;
25-
}
26-
27-
private static object Deserialize(string xml, Type type, XmlDictionaryReaderQuotas quotas)
28-
{
29-
try
30-
{
31-
var bytes = Encoding.UTF8.GetBytes(xml);
32-
33-
using (var reader = XmlDictionaryReader.CreateTextReader(bytes, quotas))
34-
{
35-
var serializer = new System.Runtime.Serialization.DataContractSerializer(type);
36-
return serializer.ReadObject(reader);
37-
}
38-
}
39-
catch (Exception ex)
40-
{
41-
throw new SerializationException("DeserializeDataContract: Error converting type: " + ex.Message, ex);
42-
}
43-
}
44-
45-
public static object DeserializeFromString(string xml, Type type)
46-
{
47-
return Deserialize(xml, type, Instance.quotas);
48-
}
49-
50-
public static T DeserializeFromString<T>(string xml)
51-
{
52-
var type = typeof(T);
53-
return (T)Deserialize(xml, type, Instance.quotas);
54-
}
55-
56-
public static T DeserializeFromReader<T>(TextReader reader)
57-
{
58-
return DeserializeFromString<T>(reader.ReadToEnd());
59-
}
60-
61-
public static T DeserializeFromStream<T>(Stream stream)
62-
{
63-
var serializer = new System.Runtime.Serialization.DataContractSerializer(typeof(T));
64-
65-
return (T)serializer.ReadObject(stream);
66-
}
67-
68-
public static object DeserializeFromStream(Type type, Stream stream)
69-
{
70-
var serializer = new System.Runtime.Serialization.DataContractSerializer(type);
71-
return serializer.ReadObject(stream);
72-
}
73-
74-
public static string SerializeToString<T>(T from)
75-
{
76-
try
77-
{
78-
using (var ms = new MemoryStream())
79-
{
80-
using (var xw = new XmlTextWriter(ms, Encoding.UTF8))
81-
{
82-
var serializer = new DataContractSerializer(from.GetType());
83-
serializer.WriteObject(xw, from);
84-
xw.Flush();
85-
ms.Seek(0, SeekOrigin.Begin);
86-
using (var reader = new StreamReader(ms))
87-
{
88-
return reader.ReadToEnd();
89-
}
90-
}
91-
}
92-
}
93-
catch (Exception ex)
94-
{
95-
throw new SerializationException(string.Format("Error serializing object of type {0}", from.GetType().FullName), ex);
96-
}
97-
}
98-
99-
public static void SerializeToWriter<T>(T value, TextWriter writer)
100-
{
101-
try
102-
{
103-
using (var xw = new XmlTextWriter(writer))
104-
{
105-
var serializer = new System.Runtime.Serialization.DataContractSerializer(typeof(T));
106-
serializer.WriteObject(xw, value);
107-
}
108-
}
109-
catch (Exception ex)
110-
{
111-
throw new SerializationException(string.Format("Error serializing object of type {0}", typeof(T).FullName), ex);
112-
}
113-
}
114-
115-
public static void CompressToStream<TXmlDto>(TXmlDto from, Stream stream)
116-
{
117-
using (var deflateStream = new DeflateStream(stream, CompressionMode.Compress))
118-
using (var xw = new XmlTextWriter(deflateStream, Encoding.UTF8))
119-
{
120-
var serializer = new System.Runtime.Serialization.DataContractSerializer(from.GetType());
121-
serializer.WriteObject(xw, from);
122-
xw.Flush();
123-
}
124-
}
125-
126-
public static void SerializeToStream(object obj, Stream stream)
127-
{
128-
using (var xw = new XmlTextWriter(stream, Encoding.UTF8))
129-
{
130-
var serializer = new System.Runtime.Serialization.DataContractSerializer(obj.GetType());
131-
serializer.WriteObject(xw, obj);
132-
}
133-
}
134-
135-
public static byte[] Compress<TXmlDto>(TXmlDto from)
136-
{
137-
using (var ms = new MemoryStream())
138-
{
139-
CompressToStream(from, ms);
140-
141-
return ms.ToArray();
142-
}
143-
}
144-
}
13+
public class XmlSerializer
14+
{
15+
private readonly XmlDictionaryReaderQuotas quotas;
16+
private static XmlWriterSettings xSettings = new XmlWriterSettings();
17+
18+
public bool OmitXmlDeclaration = false;
19+
20+
public static XmlSerializer Instance
21+
= new XmlSerializer(new XmlDictionaryReaderQuotas
22+
{
23+
MaxStringContentLength = 1024 * 1024,
24+
});
25+
26+
public XmlSerializer(XmlDictionaryReaderQuotas quotas)
27+
{
28+
this.quotas = quotas;
29+
xSettings.Encoding = Encoding.UTF8;
30+
xSettings.OmitXmlDeclaration = this.OmitXmlDeclaration;
31+
}
32+
33+
private static object Deserialize(string xml, Type type, XmlDictionaryReaderQuotas quotas)
34+
{
35+
try
36+
{
37+
var bytes = Encoding.UTF8.GetBytes(xml);
38+
39+
using (var reader = XmlDictionaryReader.CreateTextReader(bytes, quotas))
40+
{
41+
var serializer = new System.Runtime.Serialization.DataContractSerializer(type);
42+
return serializer.ReadObject(reader);
43+
}
44+
}
45+
catch (Exception ex)
46+
{
47+
throw new SerializationException("DeserializeDataContract: Error converting type: " + ex.Message, ex);
48+
}
49+
}
50+
51+
public static object DeserializeFromString(string xml, Type type)
52+
{
53+
return Deserialize(xml, type, Instance.quotas);
54+
}
55+
56+
public static T DeserializeFromString<T>(string xml)
57+
{
58+
var type = typeof(T);
59+
return (T)Deserialize(xml, type, Instance.quotas);
60+
}
61+
62+
public static T DeserializeFromReader<T>(TextReader reader)
63+
{
64+
return DeserializeFromString<T>(reader.ReadToEnd());
65+
}
66+
67+
public static T DeserializeFromStream<T>(Stream stream)
68+
{
69+
var serializer = new System.Runtime.Serialization.DataContractSerializer(typeof(T));
70+
71+
return (T)serializer.ReadObject(stream);
72+
}
73+
74+
public static object DeserializeFromStream(Type type, Stream stream)
75+
{
76+
var serializer = new System.Runtime.Serialization.DataContractSerializer(type);
77+
return serializer.ReadObject(stream);
78+
}
79+
80+
public static string SerializeToString<T>(T from)
81+
{
82+
try
83+
{
84+
using (var ms = new MemoryStream())
85+
{
86+
using (var xw = XmlTextWriter.Create(ms, xSettings))
87+
{
88+
var serializer = new DataContractSerializer(from.GetType());
89+
serializer.WriteObject(xw, from);
90+
xw.Flush();
91+
ms.Seek(0, SeekOrigin.Begin);
92+
using (var reader = new StreamReader(ms))
93+
{
94+
return reader.ReadToEnd();
95+
}
96+
}
97+
}
98+
}
99+
catch (Exception ex)
100+
{
101+
throw new SerializationException(string.Format("Error serializing object of type {0}", from.GetType().FullName), ex);
102+
}
103+
}
104+
105+
public static void SerializeToWriter<T>(T value, TextWriter writer)
106+
{
107+
try
108+
{
109+
using (var xw = new XmlTextWriter(writer))
110+
{
111+
var serializer = new System.Runtime.Serialization.DataContractSerializer(typeof(T));
112+
serializer.WriteObject(xw, value);
113+
}
114+
}
115+
catch (Exception ex)
116+
{
117+
throw new SerializationException(string.Format("Error serializing object of type {0}", typeof(T).FullName), ex);
118+
}
119+
}
120+
121+
public static void CompressToStream<TXmlDto>(TXmlDto from, Stream stream)
122+
{
123+
using (var deflateStream = new DeflateStream(stream, CompressionMode.Compress))
124+
using (var xw = new XmlTextWriter(deflateStream, Encoding.UTF8))
125+
{
126+
var serializer = new System.Runtime.Serialization.DataContractSerializer(from.GetType());
127+
serializer.WriteObject(xw, from);
128+
xw.Flush();
129+
}
130+
}
131+
132+
public static void SerializeToStream(object obj, Stream stream)
133+
{
134+
using (var xw = new XmlTextWriter(stream, Encoding.UTF8))
135+
{
136+
var serializer = new System.Runtime.Serialization.DataContractSerializer(obj.GetType());
137+
serializer.WriteObject(xw, obj);
138+
}
139+
}
140+
141+
public static byte[] Compress<TXmlDto>(TXmlDto from)
142+
{
143+
using (var ms = new MemoryStream())
144+
{
145+
CompressToStream(from, ms);
146+
147+
return ms.ToArray();
148+
}
149+
}
150+
}
145151
#endif
146152
}

0 commit comments

Comments
 (0)