forked from dotnet/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoverview.vb
209 lines (173 loc) · 6.4 KB
/
overview.vb
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
Imports System.IO
Imports System.Runtime.Serialization
Imports System.Xml
Imports System.Security.Permissions
'<snippet1>
Namespace DataContractAttributeExample
' Set the Name and Namespace properties to new values.
<DataContract(Name:="Customer", [Namespace]:="http://www.contoso.com")> _
Class Person
Implements IExtensibleDataObject
' To implement the IExtensibleDataObject interface, you must also
' implement the ExtensionData property.
Private extensionDataObjectValue As ExtensionDataObject
Public Property ExtensionData() As ExtensionDataObject _
Implements IExtensibleDataObject.ExtensionData
Get
Return extensionDataObjectValue
End Get
Set
extensionDataObjectValue = value
End Set
End Property
<DataMember(Name:="CustName")> _
Friend Name As String
<DataMember(Name:="CustID")> _
Friend ID As Integer
Public Sub New(ByVal newName As String, ByVal newID As Integer)
Name = newName
ID = newID
End Sub
End Class
Class Test
Public Shared Sub Main()
Try
WriteObject("DataContractExample.xml")
ReadObject("DataContractExample.xml")
Console.WriteLine("Press Enter to end")
Console.ReadLine()
Catch se As SerializationException
Console.WriteLine("The serialization operation failed. Reason: {0}", _
se.Message)
Console.WriteLine(se.Data)
Console.ReadLine()
End Try
End Sub
Public Shared Sub WriteObject(ByVal path As String)
' Create a new instance of the Person class and
' serialize it to an XML file.
Dim p1 As New Person("Mary", 1)
' Create a new instance of a StreamWriter
' to read and write the data.
Dim fs As New FileStream(path, FileMode.Create)
Dim writer As XmlDictionaryWriter = XmlDictionaryWriter.CreateTextWriter(fs)
Dim ser As New DataContractSerializer(GetType(Person))
ser.WriteObject(writer, p1)
Console.WriteLine("Finished writing object.")
writer.Close()
fs.Close()
End Sub
Public Shared Sub ReadObject(ByVal path As String)
' Deserialize an instance of the Person class
' from an XML file. First create an instance of the
' XmlDictionaryReader.
Dim fs As New FileStream(path, FileMode.OpenOrCreate)
Dim reader As XmlDictionaryReader = XmlDictionaryReader. _
CreateTextReader(fs, New XmlDictionaryReaderQuotas())
' Create the DataContractSerializer instance.
Dim ser As New DataContractSerializer(GetType(Person))
' Deserialize the data and read it from the instance.
Dim newPerson As Person = CType(ser.ReadObject(reader), Person)
Console.WriteLine("Reading this object:")
Console.WriteLine(String.Format("{0}, ID: {1}", newPerson.Name, newPerson.ID))
fs.Close()
End Sub
End Class
End Namespace
'</snippet1>
Namespace NewDataStuff
'<snippet2>
<DataContract()> _
Public Class Person
' This member is serialized.
<DataMember()> _
Friend FullName As String
' This is serialized even though it is private.
<DataMember()> _
Private Age As Integer
' This is not serialized because the DataMemberAttribute
' has not been applied.
Private MailingAddress As String
' This is not serialized, but the property is.
Private telephoneNumberValue As String
<DataMember()> _
Public Property TelephoneNumber() As String
Get
Return telephoneNumberValue
End Get
Set
telephoneNumberValue = value
End Set
End Property
End Class
'</snippet2>
End Namespace
Namespace SecurityConsiderations
'<snippet3>
<DataContract()> _
Public Class SpaceStationAirlock
<DataMember()> Private innerDoorOpenValue As Boolean = False
<DataMember()> Private outerDoorOpenValue As Boolean = False
Public Property InnerDoorOpen() As Boolean
Get
Return innerDoorOpenValue
End Get
Set(ByVal value As Boolean)
If (value & outerDoorOpenValue) Then
Throw New Exception("Cannot open both doors!")
Else
innerDoorOpenValue = value
End If
End Set
End Property
Public Property OuterDoorOpen() As Boolean
Get
Return outerDoorOpenValue
End Get
Set(ByVal value As Boolean)
If (value & innerDoorOpenValue) Then
Throw New Exception("Cannot open both doors!")
Else
outerDoorOpenValue = value
End If
End Set
End Property
End Class
'</snippet3>
End Namespace
Namespace XmlAndADO1
'<snippet4>
<DataContract([Namespace]:="http://schemas.contoso.com")> _
Public Class MyDataContract
<DataMember()> _
Public myDataMember As XmlElement
Public Sub TestClass()
Dim xd As New XmlDocument()
myDataMember = xd.CreateElement("myElement")
myDataMember.InnerText = "myContents"
myDataMember.SetAttribute("myAttribute", "myValue")
End Sub
End Class
'</snippet4>
End Namespace
Namespace XmlAndADO2
'<snippet5>
<DataContract([Namespace]:="http://schemas.contoso.com")> _
Public Class MyDataContract
<DataMember()> _
Public myDataMember(3) As XmlNode
Public Sub TestClass()
Dim xd As New XmlDocument()
Dim xe As XmlElement = xd.CreateElement("myElement")
xe.InnerText = "myContents"
xe.SetAttribute("myAttribute", "myValue")
Dim atr As XmlAttribute = xe.Attributes(0)
Dim cmnt As XmlComment = xd.CreateComment("myComment")
myDataMember(0) = atr
myDataMember(1) = cmnt
myDataMember(2) = xe
myDataMember(3) = xe
End Sub
End Class
'</snippet5>
End Namespace