473,411 Members | 2,014 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

How to specify the type for XmlSerializer?

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/lib...erializer.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
Jan 16 '08 #1
4 6457
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(Typ e 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(Typ e type,
MemberInfo source, Boolean directReference, Boolean throwOnError)
at System.Xml.Serialization.ModelScope.GetTypeModel(T ype type,
Boolean directReference)
at
System.Xml.Serialization.XmlReflectionImporter.Imp ortTypeMapping(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\Delete Me_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.Run UsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context( Object state)
at System.Threading.ExecutionContext.Run(ExecutionCon text
executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
Jan 16 '08 #2
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.

"Siegfried Heintze" <si*******@heintze.comwrote in message
news:OP****************@TK2MSFTNGP06.phx.gbl...
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/lib...erializer.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

Jan 16 '08 #3
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
Jan 16 '08 #4
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.
"Siegfried Heintze" <si*******@heintze.comwrote in message
news:eD**************@TK2MSFTNGP05.phx.gbl...
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
Jan 17 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: JohnnySparkles | last post by:
Hi everyone, I'm currently writing an application which uses the XmlSerializer class to serialize/deserialize objects to/from xml. Now when deserializing an XmlDocument back into the object,...
0
by: Dan Noel | last post by:
I am implementing a DataStore/Settings object that stores objects in XML by serializing the objects in XML and then associating these objects with key names. At some later point, I want to...
1
by: Bob Rock | last post by:
Hello, I have a web method that serializes a class that includes among its public fields a System.Object field to which, at runtime, I assign instances of various different classes. When...
2
by: Claire | last post by:
I'm calling a function that requires an object of class System.Type as a parameter. The example I have shows the following. System.Xml.Serialization.XmlSerializer reader = new...
0
by: Philipp Sumi | last post by:
Hi all I have a strange situation with a VS.net Add-In that uses the XmlSerializer class for deserialization. In the code below, I pass a given type to the serializer class. However, the type of...
12
by: Michael Maes | last post by:
Hello, I have a BaseClass and many Classes which all inherit (directly) from the BaseClass. One of the functions in the BaseClass is to (de)serialize the (inherited) Class to/from disk. ...
6
by: geoffrobinson | last post by:
Hi, I'm serializing an object using XmlSerializer. It is serializing, but we are getting errors upon deserialization. We use the following code to serialize: FileStream fs = new...
16
by: danielbuus | last post by:
....or, to put it perhaps more communicative, something like this: Type someObjectsType = someObject.GetType(); someObjectsType newObject = new someObjectsType(); Is this possible? If so, how?...
3
by: phil | last post by:
I have written a little application that can grab part of a page from a web site. I then want to take this result and be able to serialize it so that it can be stored as XML. I am storing these...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.