Groups | Blog | Home
all groups > dotnet xml > september 2004 >

dotnet xml : How to deserizliaes multiple records from SQLXML?


Don
9/17/2004 3:09:53 PM
Hi:

When I read my sqlxml results into a reader and deserialize it, I only get
the first record deserialized into my object.
If I have five records, they're all in the reader. But how do I get access
to all of them via an object or objects?
Do I need an object collection? Here's my XML that comes back from SQL
Server:
<Employees empid="1" lastname="Ng" firstname="William" title="Technical
Consultant" hiredate="01/01/1999"/>
<Employees empid="2" lastname="Tyson" firstname="Mike" title="Accountant"
hiredate="02/03/2001"/>

<Employees empid="3" lastname="Jones" firstname="James" title="Programmer"
hiredate="01/01/2004"/>

<Employees empid="4" lastname="Duong" firstname="Do" title="Programmer"
hiredate="12/02/1998"/>

<Employees empid="5" lastname="Kent" firstname="Moses" title="Manager"
hiredate="12/01/1999"/>

<Employees empid="6" lastname="Henny" firstname="Ray" title="Manager"
hiredate="01/02/2002"/>

Here's my current class:

<XmlRoot("Employees")> Public Class employee

<XmlAttributeAttribute()> Public empid As String = ""

<XmlAttributeAttribute()> Public firstname As String = ""

<XmlAttributeAttribute()> Public lastname As String = ""

<XmlAttributeAttribute()> Public title As String = ""

<XmlAttributeAttribute()> Public hiredate As String = ""

End Class

Thanks,

Don

Derek Harmon
9/17/2004 11:21:44 PM
[quoted text, click to view]

Deserialize() must be called one time for each object
you're deserializing. Here is an example in VB.NET,

- - - SqlXmlDeserialize.vb (excerpt)
' . . .
Dim reader As XmlReader
reader = objCmd.ExecuteXmlReader()

Dim serializer As XmlSerializer
serializer = New XmlSerializer( GetType( employee))

Dim employees As employee()
employees = New employee( 6)

Dim N As Integer
For N = 0 To employees.Length

employees( N) = CType( serializer.Deserialize( reader), employee)

If ( employees( N) Is Nothing ) Then
Console.WriteLine( "#" + CStr( N) + " is Nothing.")
Else
Console.WriteLine( "#" + CStr( N) + " " + employees( N).firstname + " " + employees( N).lastname)
End If

reader.ResetState()

Next N
' . . .
- - -

Observe the special handling of the XmlReader that is
necessary since SQLXML has multiple document elements
in it's results (which makes for a document that isn't
true XML). Unless you want the application to receive
an InvalidOperationException, you must call ResetState()
on the XmlReader after each object has been deserialized.
This call causes the reader's state to forget that it
has read a document's end element, so it can resume
it's processing anew.


Derek Harmon

Don
9/20/2004 12:00:30 PM
Derek:

I tried some of this code. The intellisense is not liking the (6) you
specified after instanting the employee object.
I am assuming this 6 specifies the number of objects to loop through. But
what do I need to do in the employee class constructor to allow it to accept
the (6).

Thanks,

Don

[quoted text, click to view]

Derek Harmon
9/20/2004 8:17:25 PM
[quoted text, click to view]

That's correct. Try this instead,

[quoted text, click to view]

Dim employees( ) As employee = { _
New employee( ), _
New employee( ), _
New employee( ), _
New employee( ), _
New employee( ), _
New employee( ) _
}


Derek Harmon

Don
9/21/2004 8:43:15 AM
If I am bring back many results from sqlxml, I won't know how many objects
are returned from the query.

[quoted text, click to view]

AddThis Social Bookmark Button