all groups > dotnet xml > september 2003 >
You're in the

dotnet xml

group:

XMLSerializer and SQL Server


XMLSerializer and SQL Server Harris Boyce
9/2/2003 7:47:55 PM
dotnet xml: Hello,

I'm trying to use the FOR XML EXPLICIT clause with SQL
Server to deserialize data from my database into a
strongly-typed collection object that I will use
throughout my application. I initially tested my design
by building a collection in code and then serializing it
to/from an XML file, which worked fine. However, I have
hit a brick wall trying to restore the data from SQL
Server. I originally had my collection and object
classes just marked as [Serializable()] without any of
the XML Serialization attributes; however, replacing
the "standard" serialization attributes with XmlRoot and
XmlAttributes (on properties) has not yielded any
success. The error I am receiving is as follows when
marked as Serializable:

Unhandled Exception: System.InvalidOperationException:
There is an error in XML
document (1, 2). ---> System.InvalidOperationException:
<Categories xmlns=''> wa
s not expected.
at
Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializa
tionReader1.Read
4_ArrayOfCategory()
--- End of inner exception stack trace ---
at System.Xml.Serialization.XmlSerializer.Deserialize
(XmlReader xmlReader, St
ring encodingStyle)
at System.Xml.Serialization.XmlSerializer.Deserialize
(XmlReader xmlReader)
at PubsData.Tester.main() in F:\Global
Documents\Visual Studio .NET Projects\
PHMCPubs\PubsData\Class1.vb:line 64

<Categories> is my root element, followed by an arbitrary
number of <Category attrib1="xyz" attrib2="123".../>
elements.

I appreciate any comments or suggestions that anyone may
have.

Thanks,

Harris

Re: XMLSerializer and SQL Server SQL Server Development Team [MSFT]
9/3/2003 11:58:12 AM
Have you read this article?
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnexxml/html/xml04212003.asp

Thanks. Mark
PM - System.Xml

--
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm.
[quoted text, click to view]

Re: XMLSerializer and SQL Server Christoph Schittko [MVP]
9/3/2003 8:58:02 PM
Harris,

can you post the code you use to deserialize and the collection/classes you
deserialize into?

--
HTH
Christoph Schittko [MVP]
Software Architect, .NET Mentor

[quoted text, click to view]

Re: XMLSerializer and SQL Server Harris Boyce
9/4/2003 10:15:39 PM
Christoph,

Thanks for your reply; below is all of the code -
collection, IEnumerator and CollectionObject that I am
using. Originally, I had tried to generate the class(es)
using XSD.exe from the XDR using XMLDATA in the stored
procedure, which created the comments you see (see my
reply to the SQL Dev Team post for the the SP code). As
a note, please keep in mind that I am using
SqlCommand.ExecuteXMLReader and not
SqlXmlCommand.ExecuteXmlReader to get the data from SQL
Server; I'm trying to make the application as dependency-
free as possible.

Thanks for your time,

Harris

-- CODE: --

Option Strict On
Option Explicit On

Imports System
Imports System.Collections
Imports System.Xml.Serialization

'
'This source code was auto-generated by xsd,
Version=1.1.4322.573.
'

'<remarks/>
'<System.Xml.Serialization.XmlRootAttribute("Categories",
[Namespace]:="", IsNullable:=False)> _
<Serializable()> _
Public Class Categories
Inherits CollectionBase

'<remarks/>
'<System.Xml.Serialization.XmlElementAttribute
("Category",
Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)> _
'Public Items() As Category

Public Sub New()
MyBase.New()
End Sub

Public Sub New(ByVal NewCategories As Categories)
MyBase.New()
AddRange(NewCategories)
End Sub

Public Sub New(ByVal arrCategories() As Category)
MyBase.New()
AddRange(arrCategories)
End Sub

Default Public Property Item(ByVal index As Integer)
As Category

Get
Return DirectCast(Me.List.Item(index),
Category)
End Get

Set(ByVal Value As Category)
Me.List.Item(index) = Value
End Set

End Property

Public Function Add(ByVal aCategory As Category) As
Integer
Return Me.List.Add(aCategory)
End Function

Public Overloads Sub AddRange(ByVal values() As
Category)

Try
For Each cat As Category In values
Me.Add(cat)
Next

Catch ex As Exception
'umm...??
End Try

End Sub

Public Overloads Sub AddRange(ByVal values As
Categories)

Try

For Each cat As Category In values
Me.Add(cat)
Next

Catch ex As Exception
'umm...???
End Try

End Sub

Public Sub CopyTo(ByVal catArray() As Category, ByVal
index As Integer)
Me.List.CopyTo(catArray, index)
End Sub

Public Function Contains(ByVal value As Category) As
Boolean
Return MyBase.List.Contains(value)
End Function

Public Function IndexOf(ByVal cat As Category) As
Integer
Return Me.List.IndexOf(cat)
End Function

Public Sub Insert(ByVal index As Integer, ByVal value
As Category)
Me.List.Insert(index, value)
End Sub

Public Sub Remove(ByVal value As Category)
Me.List.Remove(value)
End Sub

Public Shadows Function GetEnumerator() As
CategoryEnumerator
Return New CategoryEnumerator(Me)
End Function

End Class



Public Class CategoryEnumerator
Inherits Object
Implements IEnumerator

Private _baseEnumerator As IEnumerator
Private _tempEnumerable As IEnumerable

Public Sub New(ByVal mappings As Categories)

MyBase.new()
Me._tempEnumerable = DirectCast(mappings,
IEnumerable)
Me._baseEnumerator =
Me._tempEnumerable.GetEnumerator

End Sub

Public ReadOnly Property IEnumerator_Current() As
Object Implements System.Collections.IEnumerator.Current
Get
Return _baseEnumerator.Current
End Get
End Property

Public Function IEnumerator_MoveNext() As Boolean
Implements System.Collections.IEnumerator.MoveNext
Return _baseEnumerator.MoveNext()
End Function

Public Sub IEnumerator_Reset() Implements
System.Collections.IEnumerator.Reset
_baseEnumerator.Reset()
End Sub

Public ReadOnly Property Current() As Category
Get
Return DirectCast(_baseEnumerator.Current,
Category)
End Get
End Property

Public Function MoveNext() As Boolean
Return _baseEnumerator.MoveNext()
End Function

Public Sub Reset()
_baseEnumerator.Reset()
End Sub

End Class


'<remarks/>
'<XmlRoot("Category", [Namespace]:="", IsNullable:=False)
[quoted text, click to view]
<Serializable()> _
Public Class Category

'<remarks/>
Private _CategoryID As Integer
'<System.Xml.Serialization.XmlAttributeAttribute()> _
Public Property CategoryID() As Integer

Get
Return _CategoryID
End Get

Set(ByVal Value As Integer)
_CategoryID = Value
End Set

End Property

'<remarks/>
Private _CategoryName As String
'<System.Xml.Serialization.XmlAttributeAttribute()> _
Public Property CategoryName() As String

Get
Return _CategoryName
End Get

Set(ByVal Value As String)
_CategoryName = Value
End Set

End Property

'<remarks/>
Private _DisplayOrder As Integer
'<System.Xml.Serialization.XmlAttributeAttribute()> _
Public Property DisplayOrder() As Integer

Get
Return _DisplayOrder
End Get

Set(ByVal Value As Integer)
_DisplayOrder = Value
End Set

End Property

End Class


[quoted text, click to view]
AddThis Social Bookmark Button