FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
MapWindow5/src/MW5.Shared/XmlSerializationHelper.cs at develop · MapWindow/MapWindow5 · GitHub
MapWindow
/
MapWindow5
Public
Notifications
You must be signed in to change notification settings
Fork
102
Star
308
Code
Issues
14
Pull requests
12
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
MapWindow5
/
src
/
MW5.Shared
/
XmlSerializationHelper.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
147 lines (123 loc) · 4.84 KB
Breadcrumbs
MapWindow5
/
src
/
MW5.Shared
/
XmlSerializationHelper.cs
Copy path
File metadata and controls
147 lines (123 loc) · 4.84 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
// -------------------------------------------------------------------------------------------
// <copyright file="XmlSerializationHelper.cs" company="MapWindow OSS Team - www.mapwindow.org">
// MapWindow OSS Team - 2015
// </copyright>
// -------------------------------------------------------------------------------------------
using
System
;
using
System
.
Collections
.
Generic
;
using
System
.
IO
;
using
System
.
Text
;
using
System
.
Xml
;
using
System
.
Xml
.
Serialization
;
namespace
MW5
.
Shared
{
/// <summary>
/// Serializes and deserializes objects with XmlSerializer.
/// </summary>
public
static
class
XmlSerializationHelper
{
public
static
T
DeserializeFromXml
<
T
>
(
this
string
targetString
)
{
return
DeserializeFromXml
<
T
>
(
targetString
,
null
)
;
}
public
static
T
DeserializeFromXml
<
T
>
(
this
string
targetString
,
IEnumerable
<
Type
>
knownTypes
)
{
var
encoding
=
GetXmlDocEncoding
(
targetString
)
;
using
(
var
stream
=
new
MemoryStream
(
encoding
.
GetBytes
(
targetString
)
)
)
{
var
xmlDict
=
new
XmlDictionaryReaderQuotas
{
MaxStringContentLength
=
285192
}
;
using
(
var
reader
=
XmlDictionaryReader
.
CreateTextReader
(
stream
,
xmlDict
)
)
{
var
ser
=
new
XmlSerializer
(
typeof
(
T
)
)
;
return
(
T
)
ser
.
Deserialize
(
reader
)
;
}
}
}
public
static
T
DeserializeXmlElement
<
T
>
(
XmlElement
el
)
{
XmlReader
reader
=
new
XmlNodeReader
(
el
)
;
var
ser
=
new
XmlSerializer
(
typeof
(
T
)
)
;
var
o
=
ser
.
Deserialize
(
reader
)
;
return
(
T
)
o
;
}
public
static
XmlElement
SerializeToElement
<
T
>
(
this
T
target
)
{
using
(
var
stream
=
new
MemoryStream
(
)
)
{
var
ser
=
new
XmlSerializer
(
target
.
GetType
(
)
)
;
var
ns
=
new
XmlSerializerNamespaces
(
)
;
ns
.
Add
(
String
.
Empty
,
String
.
Empty
)
;
ser
.
Serialize
(
stream
,
target
,
ns
)
;
var
doc
=
new
XmlDocument
(
)
;
stream
.
Flush
(
)
;
stream
.
Position
=
0
;
doc
.
Load
(
stream
)
;
return
doc
.
DocumentElement
;
}
}
public
static
string
SerializeToXml
<
T
>
(
this
T
target
)
{
return
SerializeToXml
(
target
,
null
)
;
}
public
static
string
SerializeToXml
<
T
>
(
this
T
target
,
IEnumerable
<
Type
>
knownTypes
)
{
using
(
var
stream
=
new
MemoryStream
(
)
)
{
var
encoding
=
Encoding
.
UTF8
;
using
(
var
xmlWriter
=
new
XmlTextWriter
(
stream
,
encoding
)
)
{
xmlWriter
.
Formatting
=
Formatting
.
Indented
;
// create an empty namespace for output
var
ns
=
new
XmlSerializerNamespaces
(
)
;
ns
.
Add
(
String
.
Empty
,
String
.
Empty
)
;
var
ser
=
new
XmlSerializer
(
typeof
(
T
)
)
;
ser
.
Serialize
(
xmlWriter
,
target
,
ns
)
;
stream
.
Flush
(
)
;
stream
.
Position
=
0
;
var
sr
=
new
StreamReader
(
stream
)
;
string
result
=
sr
.
ReadToEnd
(
)
;
return
result
;
}
}
}
public
static
string
SerializeToXml
(
XmlDocument
doc
)
{
var
sb
=
new
StringBuilder
(
)
;
var
settings
=
new
XmlWriterSettings
{
Indent
=
true
,
IndentChars
=
@" "
,
NewLineChars
=
Environment
.
NewLine
,
NewLineHandling
=
NewLineHandling
.
Replace
,
}
;
using
(
var
writer
=
XmlWriter
.
Create
(
sb
,
settings
)
)
{
if
(
doc
.
ChildNodes
[
0
]
is
XmlProcessingInstruction
)
{
doc
.
RemoveChild
(
doc
.
ChildNodes
[
0
]
)
;
}
doc
.
Save
(
writer
)
;
return
sb
.
ToString
(
)
;
}
}
internal
static
Encoding
GetXmlDocEncoding
(
string
targetString
)
{
var
encoding
=
Encoding
.
UTF8
;
using
(
var
reader
=
new
XmlTextReader
(
new
StringReader
(
targetString
)
)
)
{
reader
.
Read
(
)
;
if
(
reader
.
LocalName
.
Equals
(
"xml"
,
StringComparison
.
InvariantCultureIgnoreCase
)
)
{
while
(
reader
.
MoveToNextAttribute
(
)
)
{
if
(
reader
.
Name
.
Equals
(
"encoding"
,
StringComparison
.
InvariantCultureIgnoreCase
)
)
{
encoding
=
Encoding
.
GetEncoding
(
reader
.
Value
)
;
}
}
}
}
return
encoding
;
}
}
}
Back
|
FazBrowse Home
|
New Git URL