How to specify the type for XmlSerializer?

  • Thread starter Thread starter Siegfried Heintze
  • Start date Start date
S

Siegfried Heintze

Below is a program I'm translating from C# to VB. The C# version works. I
don't understand how to specify the type on line 55. XmlSerializer wants a
type and I tried to follow the example at
http://msdn2.microsoft.com/en-us/library/system.xml.serialization.xmlserializer.aspx
but it is still not working. I' having a similar problem on line 34 where
the XmlAttributeAttribute can take a type argument.

Thanks,
Siegfried




' Begin commands to execute this file using MS.NET with bash
' vbc /out:Module1.exe /d:noprompt /debug Module1.vb /define:noprompt
' ./Module1 hello there <<EOF
' <?xml version="1.0" encoding="IBM437"?> '
5
' <Persons xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
' <persons>
' <Person>
' <first>Siegfried</first>
' <last>Heintze</last> '
10
' </Person>
' </persons>
' </Persons>
' EOF
' rm Module1.exe '
15
' rm Module1.pdb
' End commands to execute this file using MS.NET with bash

Imports System
Imports System.IO '
20
Imports System.Xml
Imports System.Xml.Serialization
Imports System.Reflection
Imports System.Runtime.InteropServices
Imports System.Collections.Generic '
25
Module Module1
<Serializable()> Public Class Person
Public Sub New()
first = "" : last = ""
End Sub '
30
Public Sub New(ByVal f As String, ByVal l As String)
first = f : last = l
End Sub
<XmlAttributeAttribute("first")> Public first As String
Public last As String '
35
End Class
Public Class Persons
Public Sub New()
persons = Nothing
End Sub '
40
Public Sub New(ByVal ps() As Person)
persons = ps
End Sub
<XmlArrayItem("Person")> Public persons As Person()
End Class '
45
Declare Function _kbhit Lib "msvcrt.dll" () As Integer
Declare Function _getch Lib "msvcrt.dll" () As Integer
Dim outp As System.IO.TextWriter = System.Console.Out
Dim inp As System.IO.TextReader = System.Console.In
Sub Main(ByVal args() As String) '
50
Try
outp.WriteLine("starting Module1.vb")
Dim persons As Persons = New Persons(New Person() { _
New Person("siegfried", "heintze")})
Dim sr As XmlSerializer = New XmlSerializer(GetType([Persons]))'
55
'Dim persons As Persons = CType(sr.Deserialize(inp), Persons)
Dim person As Person
For Each person In persons.persons
person.last = "*" + person.last + "*"
Next '
60
sr.Serialize(outp, persons)
Finally
#If noprompt Then
outp.WriteLine("All done")
#Else '
65
outp.WriteLine("hit any key: ")
_getch()
#End If
End Try
End Sub '
70
End Module
 
Ooops, I forgot the error message!

I declared class Persons to be public, what is the problem? I also
performed a "clean" thinking there might be some confusion with some older
attempts at this in other source files I was compiling in the same project.
Thanks,
Siegfried

System.InvalidOperationException was unhandled
Message="DeleteMe_Test002.Module1 is inaccessible due to its protection
level. Only public types can be processed."
Source="System.Xml"
StackTrace:
at System.Xml.Serialization.TypeDesc.CheckSupported()
at System.Xml.Serialization.TypeScope.GetTypeDesc(Type type,
MemberInfo source, Boolean directReference, Boolean throwOnError)
at System.Xml.Serialization.TypeScope.ImportTypeDesc(Type type,
MemberInfo memberInfo, Boolean directReference)
at System.Xml.Serialization.TypeScope.GetTypeDesc(Type type,
MemberInfo source, Boolean directReference, Boolean throwOnError)
at System.Xml.Serialization.ModelScope.GetTypeModel(Type type,
Boolean directReference)
at
System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(Type type,
XmlRootAttribute root, String defaultNamespace)
at System.Xml.Serialization.XmlSerializer..ctor(Type type, String
defaultNamespace)
at System.Xml.Serialization.XmlSerializer..ctor(Type type)
at DeleteMe_Test002.Module1.Main(String[] args) in
C:\WinOOP\VB.NET\Delegate_v_Event_Forms2005\DeleteMe_Test002\Module1.vb:line
55
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[]
args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence
assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
 
Move the class *outside* of Module1 or change the accessibility of Module1.
Also I beleive the attribute you ahve on the First field is incorrect givne
the XML shows it as an element, not an attribute.
 
Thanks! That fixed it!
Also I beleive the attribute you ahve on the First field is incorrect
givne the XML shows it as an element, not an attribute.
You are right. How can I change this to make it an attribute? Why is it
ignoring my attribute (or annotation)?

Thanks,
Siegfried
 
Hi Siegfried,

XMl deserialization ignores what it does not know. In this case in the
original XML it is as a element, not an attribute, so leaving he field as
public without any attribute on it will work for deserialization.
 
Back
Top