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.XmlS erializa
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 8 3417
Have you read this article? http://msdn.microsoft.com/library/de...ml04212003.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.
"Harris Boyce" <hb****@thedotworks.com> wrote in message
news:06****************************@phx.gbl... 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.XmlS erializa 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
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
"Harris Boyce" <hb****@thedotworks.com> wrote in message
news:06****************************@phx.gbl... 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.XmlS erializa 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
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("Categ ories",
[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) _
<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
-----Original Message----- Harris,
can you post the code you use to deserialize and the
collection/classes youdeserialize into?
-- HTH Christoph Schittko [MVP] Software Architect, .NET Mentor
"Harris Boyce" <hb****@thedotworks.com> wrote in message news:06****************************@phx.gbl... 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.XmlS erializa 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
.
Harris,
It appears you are selecting the something from the database which defines
the default namespace of the element, i.e. it creates an xmlns attribute
(this may just appear because of line breaks in the posting)
None of your classes identify any namespace for the XML nodes they map to,
so I suspect your problem is somewhat related to that. The exception message
complains also complains about encoutering a namespace declaration when
reading the returned XML, so there is something going on here.
However, an empty namespace declaration like your exception message cites
should not create a problem, and it didn't when I verified it.
Have you actually made sure that the Xml you get back from your stored
procedure matches the test data you had, complete with namespace
declarations, etc? You could, for example, just write the output from the
ExecuteXmlReader to the console or a file by using the WriteNode method of
an XmlTextWriter, such as:
XmlTextWriter writer = new XmlTextWriter( Console.Out );
writer.WriteStartElement( "MyRoot" );
writer.WriteNode( cmd.ExecuteXmlReader() );
writer.WriteEndElement();
--
HTH
Christoph Schittko [MVP]
Software Architect, .NET Mentor
"Harris Boyce" <hb****@thedotworks.com> wrote in message
news:26****************************@phx.gbl... Mark,
Thanks for your reply. I looked over the article before I began working on this project and took notes. However, I'm trying not to couple the application so tightly, thus, I'm looking to avoid using extra dependencies like the SQLXML Managed Classes. With that said, I attempt to deserialize the XML by calling SqlCommand.ExecuteXmlReader. I've included my stored procedure code below; the second, commented SELECT was a test without creating a root element inside the SP, hoping that the XmlSerializer would see the XML as an Array of Category objects and then I could just load the array into my strongly-typed collection. I also posted the code for the collection, IEnumerator, and "collection- object" classes in my reply to Christoph if you would like to take a gander at those as well.
Thanks for your time and ideas,
Harris
-- SP CODE: (CREATE PROCEDURE omitted) --
SELECT 1 AS tag, null AS parent, NULL AS [Categories!1!!element], --'http://tdw/' AS [Categories!1!xmlns], NULL AS [Category! 2!CategoryID], NULL AS [Category! 2!CategoryName], null AS [Category! 2!DisplayOrder]
UNION ALL
SELECT 2 AS tag, 1 AS parent, NULL AS [Categories!1!!element], --NULL AS [Categories!1!xmlns], -1 AS [Category! 2!CategoryID], 'Select Category ' AS [Category!2!CategoryName], -1 AS [Category! 2!DisplayOrder]
UNION ALL
SELECT 2 AS tag, 1 AS parent, NULL AS [Categories!1!!element], --NULL AS [Categories!1!xmlns], CategoryID AS [Category! 2!CategoryID], [Name] AS [Category! 2!CategoryName], DisplayOrder AS [Category! 2!DisplayOrder] FROM categories
ORDER BY [Category!2!DisplayOrder] -- -- SELECT 1 AS tag, -- NULL AS parent, -- -- NULL AS [Categories!1!!element], -- --NULL AS [Categories!1!xmlns], -- -1 AS [Category! 1!CategoryID], -- 'Select Category ' AS [Category!1!CategoryName], -- -1 AS [Category! 1!DisplayOrder] -- -- -- UNION ALL -- -- SELECT 1 AS tag, -- NULL AS parent, -- -- NULL AS [Categories!1!!element], -- --NULL AS [Categories!1!xmlns], -- CategoryID AS [Category! 1!CategoryID], -- ltrim(rtrim([Name])) AS [Category! 1!CategoryName], -- DisplayOrder AS [Category! 1!DisplayOrder] -- FROM categories -- -- ORDER BY [Category!1!DisplayOrder]
FOR XML EXPLICIT
-----Original Message----- 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 athttp://www.microsoft.com/info/cpyright.htm. "Harris Boyce" <hb****@thedotworks.com> wrote in message news:06****************************@phx.gbl... 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.XmlS erializa 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
.
Christoph,
OK, I have some answers...I think...I took a look at the
file that I created when I manually built a collect using
a SQLDataReader and just manually created Category
objects, added each to an instance of a Categories
collection, and serialized the data out to an XML file.
The problem is that, I think, since the classes are
marked as Serializable() and are not using the Xml*
serializtion attributes, my root node in the serialized
XML file is ArrayOfCategory and not Categories like my
FOR XML stored procedure is creating...So now I am
wondering what XML attributes I need to mark my objects
with so that I am able to serialize them the way I would
like or if I should just throw in the towel and try to
conform to the schema .NET seems to building for me.
I've included examples of both documents so you can see
what I'm referring to. Thanks so much for your time and
thoughts. I appreciate it.
H
-- -=-= BEGIN Serializable() DOCUMENT =-=- --
<ArrayOfCategory
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Category>
<CategoryID>3</CategoryID>
<CategoryName>Archaeology and Native
Americans</CategoryName>
<DisplayOrder>0</DisplayOrder>
</Category>
<Category>
<CategoryID>4</CategoryID>
<CategoryName>Architecture and Historic
Preservation</CategoryName>
<DisplayOrder>0</DisplayOrder>
</Category>
Christoph,
In a way, please disregard my previous message. I sort
of figured it out...I marked all the properties on my
Category class with XmlAttribute and marked the class
itself with XmlRootAttribute(ElementName:="Category"). I
then kept the collection class as Serializable; however,
when I created an instance of the XmlSerializer class, I
used the following constructor implementation:
XmlSerializer s = new XmlSerializer(typeof(Categories),
new XmlRootAttribute("Category"));
With those changes, I was able to serialize my data in
the format I was looking for...Problem is, I'm not sure
why it worked. I love it when that happens, don't you?
Have any ideas as to why?
Again, I can't thank you enough for all your time.
Harris -----Original Message----- Harris,
It appears you are selecting the something from the
database which definesthe default namespace of the element, i.e. it creates an
xmlns attribute(this may just appear because of line breaks in the
posting)None of your classes identify any namespace for the XML
nodes they map to,so I suspect your problem is somewhat related to that.
The exception messagecomplains also complains about encoutering a namespace
declaration whenreading the returned XML, so there is something going on
here. However, an empty namespace declaration like your
exception message citesshould not create a problem, and it didn't when I
verified it. Have you actually made sure that the Xml you get back
from your storedprocedure matches the test data you had, complete with
namespacedeclarations, etc? You could, for example, just write
the output from theExecuteXmlReader to the console or a file by using the
WriteNode method ofan XmlTextWriter, such as:
XmlTextWriter writer = new XmlTextWriter( Console.Out ); writer.WriteStartElement( "MyRoot" ); writer.WriteNode( cmd.ExecuteXmlReader() ); writer.WriteEndElement();
-- HTH Christoph Schittko [MVP] Software Architect, .NET Mentor
"Harris Boyce" <hb****@thedotworks.com> wrote in message news:26****************************@phx.gbl... Mark,
Thanks for your reply. I looked over the article
before I began working on this project and took notes.
However, I'm trying not to couple the application so tightly, thus, I'm looking to avoid using extra dependencies
like the SQLXML Managed Classes. With that said, I attempt
to deserialize the XML by calling SqlCommand.ExecuteXmlReader. I've included my stored procedure code below; the second, commented SELECT was
a test without creating a root element inside the SP, hoping that the XmlSerializer would see the XML as an Array of Category objects and then I could just load
the array into my strongly-typed collection. I also posted the code for the collection, IEnumerator,
and "collection- object" classes in my reply to Christoph if you would like to take a gander at those as well.
Thanks for your time and ideas,
Harris
-- SP CODE: (CREATE PROCEDURE omitted) --
SELECT 1 AS tag, null AS parent, NULL AS [Categories!1!!element], --'http://tdw/' AS [Categories!1!xmlns], NULL AS [Category! 2!CategoryID], NULL AS [Category! 2!CategoryName], null AS [Category! 2!DisplayOrder]
UNION ALL
SELECT 2 AS tag, 1 AS parent, NULL AS [Categories!1!!element], --NULL AS [Categories!1!xmlns], -1 AS [Category! 2!CategoryID], 'Select Category ' AS [Category!2!CategoryName], -1 AS [Category! 2!DisplayOrder]
UNION ALL
SELECT 2 AS tag, 1 AS parent, NULL AS [Categories!1!!element], --NULL AS [Categories!1!xmlns], CategoryID AS [Category! 2!CategoryID], [Name] AS [Category! 2!CategoryName], DisplayOrder AS [Category! 2!DisplayOrder] FROM categories
ORDER BY [Category!2!DisplayOrder] -- -- SELECT 1 AS tag, -- NULL AS parent, -- -- NULL AS [Categories!1!!element], -- --NULL AS [Categories!1!xmlns], -- -1 AS [Category! 1!CategoryID], -- 'Select Category ' AS [Category!1!CategoryName], -- -1 AS [Category! 1!DisplayOrder] -- -- -- UNION ALL -- -- SELECT 1 AS tag, -- NULL AS parent, -- -- NULL AS [Categories!1!!element], -- --NULL AS [Categories!1!xmlns], -- CategoryID AS [Category! 1!CategoryID], -- ltrim(rtrim([Name])) AS [Category! 1!CategoryName], -- DisplayOrder AS [Category! 1!DisplayOrder] -- FROM categories -- -- ORDER BY [Category!1!DisplayOrder]
FOR XML EXPLICIT
>-----Original Message----- >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. >"Harris Boyce" <hb****@thedotworks.com> wrote in
message >news:06****************************@phx.gbl... >> 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.XmlS erializa >> 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 >> >> > > >. >
.
Now that you mention it ...
I vaguely remember this coming up before, but I don't think it was ever
confirmed as a bug ... even though I believe it is ;)
--
HTH
Christoph Schittko [MVP]
Software Architect, .NET Mentor
"Harris Boyce" <hb****@thedotworks.com> wrote in message
news:4f****************************@phx.gbl... Christoph,
In a way, please disregard my previous message. I sort of figured it out...I marked all the properties on my Category class with XmlAttribute and marked the class itself with XmlRootAttribute(ElementName:="Category"). I then kept the collection class as Serializable; however, when I created an instance of the XmlSerializer class, I used the following constructor implementation:
XmlSerializer s = new XmlSerializer(typeof(Categories), new XmlRootAttribute("Category"));
With those changes, I was able to serialize my data in the format I was looking for...Problem is, I'm not sure why it worked. I love it when that happens, don't you?
Have any ideas as to why?
Again, I can't thank you enough for all your time.
Harris
-----Original Message----- Harris,
It appears you are selecting the something from the database which definesthe default namespace of the element, i.e. it creates an xmlns attribute(this may just appear because of line breaks in the posting)None of your classes identify any namespace for the XML nodes they map to,so I suspect your problem is somewhat related to that. The exception messagecomplains also complains about encoutering a namespace declaration whenreading the returned XML, so there is something going on here. However, an empty namespace declaration like your
exception message citesshould not create a problem, and it didn't when I verified it. Have you actually made sure that the Xml you get back
from your storedprocedure matches the test data you had, complete with namespacedeclarations, etc? You could, for example, just write the output from theExecuteXmlReader to the console or a file by using the WriteNode method ofan XmlTextWriter, such as:
XmlTextWriter writer = new XmlTextWriter( Console.Out ); writer.WriteStartElement( "MyRoot" ); writer.WriteNode( cmd.ExecuteXmlReader() ); writer.WriteEndElement();
-- HTH Christoph Schittko [MVP] Software Architect, .NET Mentor
"Harris Boyce" <hb****@thedotworks.com> wrote in message news:26****************************@phx.gbl... Mark,
Thanks for your reply. I looked over the article before I began working on this project and took notes. However, I'm trying not to couple the application so tightly, thus, I'm looking to avoid using extra dependencies like the SQLXML Managed Classes. With that said, I attempt to deserialize the XML by calling SqlCommand.ExecuteXmlReader. I've included my stored procedure code below; the second, commented SELECT was a test without creating a root element inside the SP, hoping that the XmlSerializer would see the XML as an Array of Category objects and then I could just load the array into my strongly-typed collection. I also posted the code for the collection, IEnumerator, and "collection- object" classes in my reply to Christoph if you would like to take a gander at those as well.
Thanks for your time and ideas,
Harris
-- SP CODE: (CREATE PROCEDURE omitted) --
SELECT 1 AS tag, null AS parent, NULL AS [Categories!1!!element], --'http://tdw/' AS [Categories!1!xmlns], NULL AS [Category! 2!CategoryID], NULL AS [Category! 2!CategoryName], null AS [Category! 2!DisplayOrder]
UNION ALL
SELECT 2 AS tag, 1 AS parent, NULL AS [Categories!1!!element], --NULL AS [Categories!1!xmlns], -1 AS [Category! 2!CategoryID], 'Select Category ' AS [Category!2!CategoryName], -1 AS [Category! 2!DisplayOrder]
UNION ALL
SELECT 2 AS tag, 1 AS parent, NULL AS [Categories!1!!element], --NULL AS [Categories!1!xmlns], CategoryID AS [Category! 2!CategoryID], [Name] AS [Category! 2!CategoryName], DisplayOrder AS [Category! 2!DisplayOrder] FROM categories
ORDER BY [Category!2!DisplayOrder] -- -- SELECT 1 AS tag, -- NULL AS parent, -- -- NULL AS [Categories!1!!element], -- --NULL AS [Categories!1!xmlns], -- -1 AS [Category! 1!CategoryID], -- 'Select Category ' AS [Category!1!CategoryName], -- -1 AS [Category! 1!DisplayOrder] -- -- -- UNION ALL -- -- SELECT 1 AS tag, -- NULL AS parent, -- -- NULL AS [Categories!1!!element], -- --NULL AS [Categories!1!xmlns], -- CategoryID AS [Category! 1!CategoryID], -- ltrim(rtrim([Name])) AS [Category! 1!CategoryName], -- DisplayOrder AS [Category! 1!DisplayOrder] -- FROM categories -- -- ORDER BY [Category!1!DisplayOrder]
FOR XML EXPLICIT
>-----Original Message----- >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. >"Harris Boyce" <hb****@thedotworks.com> wrote in message >news:06****************************@phx.gbl... >> 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.XmlS erializa >> 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 >> >> > > >. >
.
No, I believe it is supposed to work with the XmlRoot arrtibute attached to
the collection. You do need the XmlAttribute on your fields though, which
were commented out in the sample you posted.
I think you're approach is fine. I would consider Updategrams rather than
OPENXML if you are looking for a good way to store XML in SQL Server. They
have some bugs and limitations and get a little clunky, but most of the bugs
were fixed in SQLXML 3SP2. Send me an email and I might be able to send you
some stuff on updategrams.
--
HTH
Christoph Schittko [MVP]
Software Architect, .NET Mentor
"Harris Boyce" <hb****@thedotworks.com> wrote in message
news:47****************************@phx.gbl... So what you're saying is that the code really shouldn't work?? Excellent! I love undocumented features!!! ;)
Jokes aside, should I look at another implementation? My goal was to use the collection as the data source in the UI and then save the data in SQL using OPENXML. I have heard and read [1] accounts where the performance of the OPENXML methodology is nearly imcomparable to using the DataAdapter.Update() method. Any thoughts?
Again, thank you so much for all of your assistance in this matter; I truely appreciate it.
Harris
[1]: http://www.devx.com/dotnet/Article/16032
-----Original Message----- Now that you mention it ...
I vaguely remember this coming up before, but I don't think it was everconfirmed as a bug ... even though I believe it is ;)
-- HTH Christoph Schittko [MVP] Software Architect, .NET Mentor
"Harris Boyce" <hb****@thedotworks.com> wrote in message news:4f****************************@phx.gbl... Christoph,
In a way, please disregard my previous message. I sort of figured it out...I marked all the properties on my Category class with XmlAttribute and marked the class itself with XmlRootAttribute(ElementName:="Category"). I then kept the collection class as Serializable; however, when I created an instance of the XmlSerializer class, I used the following constructor implementation:
XmlSerializer s = new XmlSerializer(typeof(Categories), new XmlRootAttribute("Category"));
With those changes, I was able to serialize my data in the format I was looking for...Problem is, I'm not sure why it worked. I love it when that happens, don't you?
Have any ideas as to why?
Again, I can't thank you enough for all your time.
Harris
>-----Original Message----- >Harris, > >It appears you are selecting the something from the database which defines >the default namespace of the element, i.e. it creates an xmlns attribute >(this may just appear because of line breaks in the posting) >None of your classes identify any namespace for the XML nodes they map to, >so I suspect your problem is somewhat related to that. The exception message >complains also complains about encoutering a namespace declaration when >reading the returned XML, so there is something going on here. > >However, an empty namespace declaration like your exception message cites >should not create a problem, and it didn't when I verified it. > >Have you actually made sure that the Xml you get back from your stored >procedure matches the test data you had, complete with namespace >declarations, etc? You could, for example, just write the output from the >ExecuteXmlReader to the console or a file by using the WriteNode method of >an XmlTextWriter, such as: > >XmlTextWriter writer = new XmlTextWriter( Console.Out ); >writer.WriteStartElement( "MyRoot" ); >writer.WriteNode( cmd.ExecuteXmlReader() ); >writer.WriteEndElement(); > >-- >HTH >Christoph Schittko [MVP] >Software Architect, .NET Mentor > >"Harris Boyce" <hb****@thedotworks.com> wrote in message >news:26****************************@phx.gbl... >> Mark, >> >> Thanks for your reply. I looked over the article before >> I began working on this project and took notes. However, >> I'm trying not to couple the application so tightly, >> thus, I'm looking to avoid using extra dependencies like >> the SQLXML Managed Classes. With that said, I attempt to >> deserialize the XML by calling >> SqlCommand.ExecuteXmlReader. I've included my stored >> procedure code below; the second, commented SELECT was a >> test without creating a root element inside the SP, >> hoping that the XmlSerializer would see the XML as an >> Array of Category objects and then I could just load the >> array into my strongly-typed collection. I also posted >> the code for the collection, IEnumerator, and "collection- >> object" classes in my reply to Christoph if you would >> like to take a gander at those as well. >> >> Thanks for your time and ideas, >> >> Harris >> >> -- SP CODE: (CREATE PROCEDURE omitted) -- >> >> SELECT 1 AS tag, >> null AS parent, >> NULL AS >> [Categories!1!!element], >> --'http://tdw/' AS >> [Categories!1!xmlns], >> NULL AS [Category! >> 2!CategoryID], >> NULL AS [Category! >> 2!CategoryName], >> null AS [Category! >> 2!DisplayOrder] >> >> UNION ALL >> >> SELECT 2 AS tag, >> 1 AS parent, >> NULL AS >> [Categories!1!!element], >> --NULL AS >> [Categories!1!xmlns], >> -1 AS [Category! >> 2!CategoryID], >> 'Select Category ' AS >> [Category!2!CategoryName], >> -1 AS [Category! >> 2!DisplayOrder] >> >> >> UNION ALL >> >> SELECT 2 AS tag, >> 1 AS parent, >> NULL AS >> [Categories!1!!element], >> --NULL AS >> [Categories!1!xmlns], >> CategoryID AS [Category! >> 2!CategoryID], >> [Name] AS [Category! >> 2!CategoryName], >> DisplayOrder AS [Category! >> 2!DisplayOrder] >> FROM categories >> >> ORDER BY [Category!2!DisplayOrder] >> -- >> -- SELECT 1 AS >> tag, >> -- NULL AS parent, >> -- -- NULL AS >> [Categories!1!!element], >> -- --NULL AS >> [Categories!1!xmlns], >> -- -1 AS [Category! >> 1!CategoryID], >> -- 'Select Category ' AS >> [Category!1!CategoryName], >> -- -1 AS [Category! >> 1!DisplayOrder] >> -- >> -- >> -- UNION ALL >> -- >> -- SELECT 1 AS >> tag, >> -- NULL AS parent, >> -- -- NULL AS >> [Categories!1!!element], >> -- --NULL AS >> [Categories!1!xmlns], >> -- CategoryID AS [Category! >> 1!CategoryID], >> -- ltrim(rtrim([Name])) AS [Category! >> 1!CategoryName], >> -- DisplayOrder AS [Category! >> 1!DisplayOrder] >> -- FROM categories >> -- >> -- ORDER BY [Category!1!DisplayOrder] >> >> FOR XML EXPLICIT >> >> >-----Original Message----- >> >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. >> >"Harris Boyce" <hb****@thedotworks.com> wrote in message >> >news:06****************************@phx.gbl... >> >> 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.XmlS erializa >> >> 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 >> >> >> >> >> > >> > >> >. >> > > > >. >
. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Kevin Conroy |
last post by:
I'm making a 6-tiered application that is using Xml to tie the various
layers together in a very clean manner. I'm also trying to do this on the
1.1 Framework although I'm willing to switch to the...
|
by: Magnus Lindhe |
last post by:
Hello,
I'm trying to deserialize an XML document into a objects with the
XmlSerializer which works fine in my NUnit test suit but fails with a
TypeLoadException in the server. The difference...
|
by: SJD |
last post by:
I've just read Christoph Schittko's article on XmlSerializer:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnxmlnet/html/trblshtxsd.asp
. . . and very informative it is too....
|
by: Don McNamara |
last post by:
Hi,
I've hit quite a strange problem with XmlSerializer on my W2K3 server. When
I serialize/deserialize using an exe on my local computer (XP), everything
works fine. When I put the code out on...
|
by: Ultrakorne |
last post by:
hi, i have some problems with my client talk to my server...
i am using xmlserializer to serialize object and send them to the other
side of the connection. I need to send / recive by both client...
|
by: cd~ |
last post by:
I can provide a test app, the news server won't allow me to post the files
because they are too large (93KB and 1.2KB)
I downloaded the ESRI ArcXml schema and generated the classes from the...
|
by: Henrik Dahl |
last post by:
Hello!
I have an xml schema which has a date typed attribute. I have used xsd.exe
to create a class library for XmlSerializer. The result of
XmlSerializer.Serialize(...) should be passed as the...
|
by: kimtherkelsen |
last post by:
Hi,
I want to send XML data from a server to some clients over a network
connection using the TCP/IP protocol. If I send the XMLs as byte
arrays I need to insert header information in the data to...
|
by: theonlydavewilliams |
last post by:
Hi there
I'm hoping there's an easy answer to a (hopefully) not too long-winded issue...
I'm building a C# web client using a proxy wsdl.exe'd from a wsdl file and six schemas, each in a different...
|
by: Naresh1 |
last post by:
What is WebLogic Admin Training?
WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
|
by: jalbright99669 |
last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made but the http to https rule only works for...
|
by: antdb |
last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine
In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
|
by: Matthew3360 |
last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function.
Here is my code.
header("Location:".$urlback);
Is this the right layout the...
|
by: Matthew3360 |
last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it so the python app could use a http request to get...
|
by: Arjunsri |
last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
|
by: WisdomUfot |
last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
|
by: BLUEPANDA |
last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
|
by: Rahul1995seven |
last post by:
Introduction:
In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
| |