Question : Deserialize a Contact

The Xml below is a response from a web service.   Which I receive as a STRING.  How would I dezerialize this to its equlivent object.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
<?xml version="1.0" encoding="UTF-8"?>
<response>
	<contacts>
		<contact>
			<contactId>16006903</contactId>
			<prefix>MR</prefix>
			<firstName>Kevin</firstName>
			<lastName>Robinson</lastName>
			<suffix>PHD</suffix>
			<street>129 Ormeau Road</street>
			<street2 />
			<city>Belfast</city>
			<state>Antrim</state>
			<postalCode />
			<phone>7908790790</phone>
			<fax />
			<business />
			<email>[email protected]</email>
			<createDate>2010-07-26 10:38:22</createDate>
			<bounceCount />
			<status>normal</status>
		</contact>
	</contacts>
</response>

Answer : Deserialize a Contact

Take your XML Structure and save it to a file to let's say "c:\MyContacts.xml"
generate an XSD Schema with xsd.exe:

xsd /c:\MyContacts.xml /c

Then take the generated xsd file (called MyContacts.xsd) and generate a class structure using xsd.exe:

xsd MyContacts.xsd /classes /l:vb

This generates a class called MyContacts.vb.
This class represents a structure that corresponds to your XML Structure.

NOW you can get the response of your webservice and deserialize it with your generated class
and you will get a structure that you can work with :)

This can be done like that:

Dim MyCont As New response() 'most upper class that is generated
Dim x As New XmlSerializer(MyCont.GetType()) 'get a new xml serializer object
Dim rdr As New StreamReader("c:\Mycontacts.xml") ' load the content (in your sample it will be the response of your webservice!)

MyCont = x.Deserialize(rdr) 'deserialize the content

Dim name As String = MyCont.Items(0).contact(0).firstName 'access the data


Btw.: you can do that via Visual Studio also, if you know the WSDL of the webservice.
The manual generation done by xsd.exe is done by VS itself.

Hope that helps
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:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
260:
261:
262:
263:
264:
265:
266:
267:
268:
269:
270:
271:
272:
273:
274:
275:
276:
277:
278:
279:
280:
281:
282:
283:
284:
285:
286:
287:
288:
289:
290:
291:
292:
293:
294:
295:
'Generated via XSD:

'------------------------------------------------------------------------------
' <auto-generated>
'     Dieser Code wurde von einem Tool generiert.
'     Laufzeitversion:2.0.50727.3603
'
'     Änderungen an dieser Datei können falsches Verhalten verursachen und gehen verloren, wenn
'     der Code erneut generiert wird.
' </auto-generated>
'------------------------------------------------------------------------------

Option Strict Off
Option Explicit On

Imports System.Xml.Serialization

'
'Dieser Quellcode wurde automatisch generiert von xsd, Version=2.0.50727.42.
'

'''<remarks/>
<System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42"),  _
 System.SerializableAttribute(),  _
 System.Diagnostics.DebuggerStepThroughAttribute(),  _
 System.ComponentModel.DesignerCategoryAttribute("code"),  _
 System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=true),  _
 System.Xml.Serialization.XmlRootAttribute([Namespace]:="", IsNullable:=false)>  _
Partial Public Class response
    
    Private itemsField() As responseContacts
    
    '''<remarks/>
    <System.Xml.Serialization.XmlElementAttribute("contacts", Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)>  _
    Public Property Items() As responseContacts()
        Get
            Return Me.itemsField
        End Get
        Set
            Me.itemsField = value
        End Set
    End Property
End Class

'''<remarks/>
<System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42"),  _
 System.SerializableAttribute(),  _
 System.Diagnostics.DebuggerStepThroughAttribute(),  _
 System.ComponentModel.DesignerCategoryAttribute("code"),  _
 System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=true)>  _
Partial Public Class responseContacts
    
    Private contactField() As responseContactsContact
    
    '''<remarks/>
    <System.Xml.Serialization.XmlElementAttribute("contact", Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)>  _
    Public Property contact() As responseContactsContact()
        Get
            Return Me.contactField
        End Get
        Set
            Me.contactField = value
        End Set
    End Property
End Class

'''<remarks/>
<System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42"),  _
 System.SerializableAttribute(),  _
 System.Diagnostics.DebuggerStepThroughAttribute(),  _
 System.ComponentModel.DesignerCategoryAttribute("code"),  _
 System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=true)>  _
Partial Public Class responseContactsContact
    
    Private contactIdField As String
    
    Private prefixField As String
    
    Private firstNameField As String
    
    Private lastNameField As String
    
    Private suffixField As String
    
    Private streetField As String
    
    Private street2Field As String
    
    Private cityField As String
    
    Private stateField As String
    
    Private postalCodeField As String
    
    Private phoneField As String
    
    Private faxField As String
    
    Private businessField As String
    
    Private emailField As String
    
    Private createDateField As String
    
    Private bounceCountField As String
    
    Private statusField As String
    
    '''<remarks/>
    <System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)>  _
    Public Property contactId() As String
        Get
            Return Me.contactIdField
        End Get
        Set
            Me.contactIdField = value
        End Set
    End Property
    
    '''<remarks/>
    <System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)>  _
    Public Property prefix() As String
        Get
            Return Me.prefixField
        End Get
        Set
            Me.prefixField = value
        End Set
    End Property
    
    '''<remarks/>
    <System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)>  _
    Public Property firstName() As String
        Get
            Return Me.firstNameField
        End Get
        Set
            Me.firstNameField = value
        End Set
    End Property
    
    '''<remarks/>
    <System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)>  _
    Public Property lastName() As String
        Get
            Return Me.lastNameField
        End Get
        Set
            Me.lastNameField = value
        End Set
    End Property
    
    '''<remarks/>
    <System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)>  _
    Public Property suffix() As String
        Get
            Return Me.suffixField
        End Get
        Set
            Me.suffixField = value
        End Set
    End Property
    
    '''<remarks/>
    <System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)>  _
    Public Property street() As String
        Get
            Return Me.streetField
        End Get
        Set
            Me.streetField = value
        End Set
    End Property
    
    '''<remarks/>
    <System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)>  _
    Public Property street2() As String
        Get
            Return Me.street2Field
        End Get
        Set
            Me.street2Field = value
        End Set
    End Property
    
    '''<remarks/>
    <System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)>  _
    Public Property city() As String
        Get
            Return Me.cityField
        End Get
        Set
            Me.cityField = value
        End Set
    End Property
    
    '''<remarks/>
    <System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)>  _
    Public Property state() As String
        Get
            Return Me.stateField
        End Get
        Set
            Me.stateField = value
        End Set
    End Property
    
    '''<remarks/>
    <System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)>  _
    Public Property postalCode() As String
        Get
            Return Me.postalCodeField
        End Get
        Set
            Me.postalCodeField = value
        End Set
    End Property
    
    '''<remarks/>
    <System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)>  _
    Public Property phone() As String
        Get
            Return Me.phoneField
        End Get
        Set
            Me.phoneField = value
        End Set
    End Property
    
    '''<remarks/>
    <System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)>  _
    Public Property fax() As String
        Get
            Return Me.faxField
        End Get
        Set
            Me.faxField = value
        End Set
    End Property
    
    '''<remarks/>
    <System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)>  _
    Public Property business() As String
        Get
            Return Me.businessField
        End Get
        Set
            Me.businessField = value
        End Set
    End Property
    
    '''<remarks/>
    <System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)>  _
    Public Property email() As String
        Get
            Return Me.emailField
        End Get
        Set
            Me.emailField = value
        End Set
    End Property
    
    '''<remarks/>
    <System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)>  _
    Public Property createDate() As String
        Get
            Return Me.createDateField
        End Get
        Set
            Me.createDateField = value
        End Set
    End Property
    
    '''<remarks/>
    <System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)>  _
    Public Property bounceCount() As String
        Get
            Return Me.bounceCountField
        End Get
        Set
            Me.bounceCountField = value
        End Set
    End Property
    
    '''<remarks/>
    <System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)>  _
    Public Property status() As String
        Get
            Return Me.statusField
        End Get
        Set
            Me.statusField = value
        End Set
    End Property
End Class
Random Solutions  
 
programming4us programming4us