all groups > dotnet xml > january 2005 >
You're in the

dotnet xml

group:

Still Problems Serializing Arrays


Still Problems Serializing Arrays Wayne Wengert
1/28/2005 7:41:06 AM
dotnet xml:
I am still stuck trying to create a Class to use for exporting and importing
array data to/from XML. The format of the XML that I want to import/export
is shown below as is the Class and the code I am using to create a sample
XML file. I am trying to dimension the ArrayOfJudgeEntity to have two sets
of the JudgeTableEntity values. When I run the code I get an error that the
XML is not correct.

I jsut can't get my head around the array serialization.

Any help is much appreciated

Wayne

=================== Desired XML ==============
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfJudgeTableEntity xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<JudgeTableEntity>
<JudgeId>63371991-cdf9-4cc5-99c1-a42c599498f8</JudgeId>
<JudgeFirstName>Kristen</JudgeFirstName>
<JudgeLastName>O'Melia</JudgeLastName>
<JudgeType>Equipment</JudgeType>
</JudgeTableEntity>
<JudgeTableEntity>
<JudgeId>ce2c0c6e-4094-43da-9d8c-9726d81b017e</JudgeId>
<JudgeFirstName>Smith</JudgeFirstName>
<JudgeLastName>Dan</JudgeLastName>
<JudgeType>Movement</JudgeType>
</JudgeTableEntity>
</ArrayOfJudgeTableEntity>

============= Class Definition =============
Public Class ArrayOfJudgeTableEntity
'<XmlArray("JudgeTableEntity"), XmlArrayItem("JudgeTableEntity",
IsNullable:=True)> Public JudgeTableEntityX() As JudgeTableEntity
<XmlElement("JudgeTableEntity", IsNullable:=True)> Public
JudgeTableEntity()
Public Sub New()
End Sub
Public Sub New(ByVal j As Integer)
ReDim Preserve JudgeTableEntity(j)
End Sub 'New
End Class
Public Class JudgeTableEntity
<XmlElement("JudgeID")> Public JudgeID As String
<XmlElement("JudgeFirstName")> Public JudgeFirstName As String
<XmlElement("JudgeLastName")> Public JudgeLastName As String
<XmlElement("JudgeType")> Public JudgeType As String
Public Sub New()
End Sub 'New
Public Sub New(ByVal id As String, ByVal fn As String, ByVal ln As
String, ByVal type As String)
JudgeID = id
JudgeType = type
JudgeFirstName = fn
JudgeLastName = ln
End Sub
End Class

================ Code to Build Sample Output =================
Dim Judge1 As New JudgeTableEntity("ID1", "Shirlee", "Whitcomb", "GE")
Dim Judge2 As New JudgeTableEntity("ID2", "Joe", "Courtney", "TaP")
Dim array2 As New ArrayOfJudgeTableEntity(1)
array2.JudgeTableEntity(0) = Judge1
array2.JudgeTableEntity(1) = Judge2
Dim ser As New XmlSerializer(GetType(ArrayOfJudgeTableEntity))
AddHandler ser.UnknownNode, AddressOf serializer_UnknownNode
AddHandler ser.UnknownElement, AddressOf serializer_UnknownElement
Dim writer As New XmlTextWriter("c:\Temp\judgestest.xml",
System.Text.Encoding.UTF8)
' write a human readable file
writer.Formatting = Formatting.Indented
Try
ser.Serialize(writer, array2)
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "Error")
'End
End Try
writer.Close()

RE: Still Problems Serializing Arrays Christoph Schittko [MVP]
1/31/2005 11:37:03 AM
Wayne,

I took your sample XML, inferred a schema with xsd.exe, which ships as part
of the .NET framework, and generated classes that can deserialize XML that
conforms to the schema ... see code below.

There are other tools to infer schemas and create .NET classes for
serialization as well. Take a look at the XML tools page on gotdotnet for the
links.

Each tool as their strengths and weaknesses, but they may get you started in
the right direction.


You're issue right now seems to be that you are trying to deserialize an
array of ArrayOfJudgeTableEntities. Does the XML you are trying to
deserialize contain a root element for the ArrayOfJudgeTableEntities items or
are you reading a continuous stream to ArrayOfJudgeTableEntities elements,
which would be an XML document fragment, not a well formed document.

HTH,
Christoph Schittko
MVP XML
http://weblogs.asp.net/cschittko


//------------------------------------------------------------------------------
// <autogenerated>
// This code was generated by a tool.
// Runtime Version: 1.1.4322.573
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </autogenerated>
//------------------------------------------------------------------------------

//
// This source code was auto-generated by xsd, Version=1.1.4322.573.
//
using System.Xml.Serialization;


/// <remarks/>
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
public class ArrayOfJudgeTableEntity {

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("JudgeTableEntity",
Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public ArrayOfJudgeTableEntityJudgeTableEntity[] Items;
}

/// <remarks/>
public class ArrayOfJudgeTableEntityJudgeTableEntity {

/// <remarks/>

[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string JudgeId;

/// <remarks/>

[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string JudgeFirstName;

/// <remarks/>

[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string JudgeLastName;

/// <remarks/>

[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string JudgeType;
}



[quoted text, click to view]
Re: Still Problems Serializing Arrays Wayne Wengert
2/1/2005 6:35:53 AM
Christoph;

Thanks for the response. You are right that I need to learn more about the
available tools. I'll check out that link and review your suggestions.

The ArrayOfJudgeTableEntities is the root element (occurs only once). There
are many JudgeTableEntity sets within that root.

Wayne


"Christoph Schittko [MVP]" <Christoph Schittko
[quoted text, click to view]
//--------------------------------------------------------------------------
----
[quoted text, click to view]
//--------------------------------------------------------------------------
----
[quoted text, click to view]
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSche
maForm.Unqualified)]
[quoted text, click to view]
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSche
maForm.Unqualified)]
[quoted text, click to view]
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSche
maForm.Unqualified)]
[quoted text, click to view]
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSche
maForm.Unqualified)]
[quoted text, click to view]

Re: Still Problems Serializing Arrays Christoph Schittko [MVP]
2/1/2005 8:47:38 PM
Wayne,

In general, you should always be able to deserialize XML written with
the XmlSerializer into instances of the same classes you used to
serialize.
Did you get it to work or do you still have problems? On a side note,
I'm not a VB expert, but does your code compile with Option Strict on?

HTH,
Christoph Schittko
MVP XML
http://weblogs.asp.net/cschittko



[quoted text, click to view]

Re: Still Problems Serializing Arrays Wayne Wengert
2/1/2005 11:30:55 PM
Christoph;

Not working yet - I have another crisis that is taking most of my time for
the next day or so. Yes, Option Strict is on.

Thanks for the continued help!

Wayne

[quoted text, click to view]

AddThis Social Bookmark Button