473,757 Members | 2,284 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Deserializing objects into a different class names

Hello All,

I use the following method to serialize a settings class to a string:

'============== =============== =============== =============== ============
Public Shared Function DeflateClass(By Val serializing As Object, ByVal
typeSerializing As System.Type) As String

Dim writer As StringWriter = New StringWriter
Dim serializer As XmlSerializer = _
New XmlSerializer(t ypeSerializing)

serializer.Seri alize(writer, serializing)

Return writer.ToString ()

End Function
'============== =============== =============== =============== ============

And I use the following method to deserialize a string into a
settings class:

'============== =============== =============== =============== ============
Public Shared Function InflateClass(By Val serialized As String, ByVal
typeSerialized As System.Type) As Object

Dim serializer As XmlSerializer = _
New XmlSerializer(t ypeSerialized)
Dim reader As StringReader = _
New StringReader(se rialized)

Return serializer.Dese rialize(reader)

End Function
'============== =============== =============== =============== ============

Everything works good, BUT ... the name of my settings class has
changed. So now when I try and deserialize existing data I get:

System.InvalidO perationExcepti on: There is an error in XML document
(2, 2). ---> System.InvalidO perationExcepti on: <MySettings xmlns=''>
was not expected.
at Microsoft.Xml.S erialization.Ge neratedAssembly .XmlSerializati onReader1.Read4 _ExecuterSettin gs()
--- End of inner exception stack trace ---

This is because the serialized XML root node name (MySettings) does
not match the settings class name (ProcessSetting s). I can easily fix
the problem by editing the XML and changing the name of the root node
(from MySettings to ProcessSettings ).

Now, there must be a better way, right?

I did some research and found the SerializationBi nder class. However,
this seems to only work with the BinaryFormatter or the SoapFormatter
and not with the XmlSerializer. So, what I have ended up with is the
following method:

'============== =============== =============== =============== ============
Public Shared Function InflateClass(By Val serialized As String, ByVal
typeSerialized As System.Type) As Object

Dim existingDoc As XmlDocument = New XmlDocument
existingDoc.Loa dXml(serialized )
Dim existingRoot As XmlNode = existingDoc.Doc umentElement

Dim newDoc As XmlDocument = New XmlDocument
Dim newRoot As XmlNode = newDoc.CreateEl ement(typeSeria lized.Name)
newRoot.InnerXm l = existingRoot.In nerXml
newDoc.AppendCh ild(newRoot)

serialized = newDoc.OuterXml ()

Dim serializer As XmlSerializer = _
New XmlSerializer(t ypeSerialized)
Dim reader As StringReader = New StringReader(se rialized)
Return serializer.Dese rialize(reader)

End Function
'============== =============== =============== =============== ============

This works "okay", but I am looking for a better solution. Maybe I am
stuck with this if I continue to use the XmlSerializer instead of
SoapFormatter.

Has anyone had this problem with the XmlSerializer and found a better
solution?

Thanks for any help you can give :)
Nov 20 '05 #1
2 2451
Drolem,
Now, there must be a better way, right? Not necessarily. ;-)
This is because the serialized XML root node name (MySettings) does
not match the settings class name (ProcessSetting s). I can easily fix
the problem by editing the XML and changing the name of the root node
(from MySettings to ProcessSettings ). Have you considered using an XSLT transform to enable the program to change
the MySettings node to ProcessSettings ?

I have not done a lot with XML serialization, I understand that you can use
the System.Xml.Seri alization.XmlRo otAttribute on your class to control the
root name of the XML created. There are a number of other attributes in
System.Xml.Seri alization that you may find helpful.

Hope this helps
Jay
"Drolem" <ra****@hotmail .com> wrote in message
news:b7******** *************** ***@posting.goo gle.com... Hello All,

I use the following method to serialize a settings class to a string:

'============== =============== =============== =============== ============
Public Shared Function DeflateClass(By Val serializing As Object, ByVal
typeSerializing As System.Type) As String

Dim writer As StringWriter = New StringWriter
Dim serializer As XmlSerializer = _
New XmlSerializer(t ypeSerializing)

serializer.Seri alize(writer, serializing)

Return writer.ToString ()

End Function
'============== =============== =============== =============== ============

And I use the following method to deserialize a string into a
settings class:

'============== =============== =============== =============== ============
Public Shared Function InflateClass(By Val serialized As String, ByVal
typeSerialized As System.Type) As Object

Dim serializer As XmlSerializer = _
New XmlSerializer(t ypeSerialized)
Dim reader As StringReader = _
New StringReader(se rialized)

Return serializer.Dese rialize(reader)

End Function
'============== =============== =============== =============== ============

Everything works good, BUT ... the name of my settings class has
changed. So now when I try and deserialize existing data I get:

System.InvalidO perationExcepti on: There is an error in XML document
(2, 2). ---> System.InvalidO perationExcepti on: <MySettings xmlns=''>
was not expected.
at Microsoft.Xml.S erialization.Ge neratedAssembly .XmlSerializati onReader1.Read4 _
ExecuterSetting s() --- End of inner exception stack trace ---

This is because the serialized XML root node name (MySettings) does
not match the settings class name (ProcessSetting s). I can easily fix
the problem by editing the XML and changing the name of the root node
(from MySettings to ProcessSettings ).

Now, there must be a better way, right?

I did some research and found the SerializationBi nder class. However,
this seems to only work with the BinaryFormatter or the SoapFormatter
and not with the XmlSerializer. So, what I have ended up with is the
following method:

'============== =============== =============== =============== ============
Public Shared Function InflateClass(By Val serialized As String, ByVal
typeSerialized As System.Type) As Object

Dim existingDoc As XmlDocument = New XmlDocument
existingDoc.Loa dXml(serialized )
Dim existingRoot As XmlNode = existingDoc.Doc umentElement

Dim newDoc As XmlDocument = New XmlDocument
Dim newRoot As XmlNode = newDoc.CreateEl ement(typeSeria lized.Name)
newRoot.InnerXm l = existingRoot.In nerXml
newDoc.AppendCh ild(newRoot)

serialized = newDoc.OuterXml ()

Dim serializer As XmlSerializer = _
New XmlSerializer(t ypeSerialized)
Dim reader As StringReader = New StringReader(se rialized)
Return serializer.Dese rialize(reader)

End Function
'============== =============== =============== =============== ============

This works "okay", but I am looking for a better solution. Maybe I am
stuck with this if I continue to use the XmlSerializer instead of
SoapFormatter.

Has anyone had this problem with the XmlSerializer and found a better
solution?

Thanks for any help you can give :)

Nov 20 '05 #2
Ya, it seems like the System.Xml.Seri alization.XmlRo otAttribute will
help in writing the classes. My main problem is I have lots of these
Xml files to convert and some older processes still using the old
format.

Well, looks like I am stuck until I can upgrade my older software to
use a newer format.

Thanks for your info :)
"Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> wrote in message news:<Ok******* ******@TK2MSFTN GP11.phx.gbl>.. .
Drolem,
Now, there must be a better way, right?

Not necessarily. ;-)
This is because the serialized XML root node name (MySettings) does
not match the settings class name (ProcessSetting s). I can easily fix
the problem by editing the XML and changing the name of the root node
(from MySettings to ProcessSettings ).

Have you considered using an XSLT transform to enable the program to change
the MySettings node to ProcessSettings ?

I have not done a lot with XML serialization, I understand that you can use
the System.Xml.Seri alization.XmlRo otAttribute on your class to control the
root name of the XML created. There are a number of other attributes in
System.Xml.Seri alization that you may find helpful.

Hope this helps
Jay

Nov 20 '05 #3

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

Similar topics

1
2055
by: Justin Armstrong | last post by:
I'm having difficulties deserializing some objects. Consider the following example of what I'm trying to do: ---------------------------------------------------------- class Person { string name; } class Info {
0
2229
by: Kenneth Baltrinic | last post by:
I am getting the following error when deserializing an object that has a couple of dozen dependant objects in its object graph. Anyone who can suggest where I might begin to look to resolve problem I would greatly in debted to. Serializing the object works fine. When I try to deserialize it, I get the following error: A first chance exception of type 'System.Runtime.Serialization.SerializationException' occurred in mscorlib.dll Additional...
4
7512
by: Wayne Wengert | last post by:
Using VB.NET I want to read in an XML file that has an array of objects and then step through the resulting array in code. I build a class to define the structure and I am running code to read in the data but I can't figure out where the data is in the resulting array. Most of the relevant code is below. When I run the code to desrialize I get no errors but if I try to look at some of the data via the command window I get errors such as...
0
1991
by: Casey | last post by:
So I'm using XmlSerializer to serialize out a wrapper object that contains an arbitrary number of other objects. The class definitions listed below are made to be very generic. Some of the objects contain other objects so when I declare my XmlSerializer I need to send along an array of types to be included in the serialization. That works fine until I run into 2 classes with the same name from different asseblies. This causes an XML...
161
7869
by: KraftDiner | last post by:
I was under the assumption that everything in python was a refrence... so if I code this: lst = for i in lst: if i==2: i = 4 print lst I though the contents of lst would be modified.. (After reading that
0
7815
by: Sivajee Akula | last post by:
Hello All, I am trying to consume a .NET Service from Adobe LiveCycle Workflow. The service deals with complex objects. I am getting the following exception at the time of invocation of the service, and due to which my workflow gets stalled. When I searched the net, I found many posts reporting this error, but none with a solution. There is no code involved in the invocation, everything is handled by Adobe tool itself. I just specify the...
8
1940
by: rpsetzer | last post by:
I have to create a big web application and I was thinking of using a data layer. For each entity in the database, I'll define a class that maps the table structure, having sub-objects for each foreign key, having insert/delete/update methods, the usual deal. Yet, I am very concerned about performance. For example, there are lots of cases when I may just be needing the employee name. Yet using this model, I will have to instantiate an...
1
11816
by: =?Utf-8?B?SmVyZW15X0I=?= | last post by:
I am working on an order entry program and have a question related to deserializing nodes with nested elements. The purchase order contains multiple line items which I select using an XmlNodeList. I am trying to deserialize the nodes using a foreach as follows: foreach(XmlNode lineItem in LineItemsNodeList) An abbreviated example of the nested lineItem node looks like this:
14
6023
by: Jess | last post by:
Hello, I learned that there are five kinds of static objects, namely 1. global objects 2. object defined in namespace scope 3. object declared static instead classes 4. objects declared static inside functions (i.e. local static objects) 5. objects declared at file scope.
0
9487
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10069
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9904
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8736
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7285
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6556
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5168
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5324
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2697
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.