473,388 Members | 1,355 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,388 software developers and data experts.

XML Serialization - Help Please

!NoItAll
297 100+
I'm writing a test application in order to figure out how to serialize a structure to XML. I can not get the following code to work for me and it's not obvious to me (I'm totally new to serialization). I've scoured google for tips, but have found no real help as many of the "examples" look very similar to mine (except the sub array in one of my structures...)

The following defines the structures I am attempting to serialize. I think I have the xml decoration correct - but the errors I'm receiving (see below) don't really give me much information.
Expand|Select|Wrap|Line Numbers
  1. Imports System.IO
  2. Imports System.Xml.Serialization
  3.  
  4. Public Module modTestSerialization
  5.  
  6.     Friend ActiveRO() As ActiveROType
  7.     Friend ActiveROCount As Integer
  8.     Friend bNoReEntry As Boolean = False
  9.  
  10.     <Serializable()> Public Structure ActiveROStoriesType
  11.         <XmlAttribute("StorySlug")> Public StorySlug As String
  12.         <XmlAttribute("StoryStoryID")> Public StoryID As String
  13.         <XmlElement("StoryBody")> Public StoryBody As String
  14.     End Structure
  15.  
  16.     <Serializable()> Public Structure ActiveROType
  17.         <XmlAttribute("ROid")> Public ROID As String
  18.         <XmlAttribute("ROName")> Public ROName As String
  19.         <XmlArrayItem("ROStories")> Public ROStories() As ActiveROStoriesType
  20.     End Structure
  21.  
  22.     Friend Function UpdateROInfo(ByVal AllROs() As ActiveROType) As Boolean
  23.         Dim fs As New FileStream("c:\mos\queue\fromNCS\ActiveROTest.xml", FileMode.Create)
  24.         Dim xs As New XmlSerializer(GetType(ActiveROType))
  25.         xs.Serialize(fs, AllROs)
  26.         fs.Close()
  27.     End Function
  28. End Module
  29.  
I call the code from the following (a simple button click in a form):
Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  3.         Static I As Integer = -1
  4.  
  5.         I += 1
  6.         ReDim Preserve ActiveRO(I)
  7.         ReDim Preserve ActiveRO(I).ROStories(2)
  8.         With ActiveRO(I)
  9.             .ROID = "RO" & I.ToString
  10.             .ROName = "MyRunningOrder-" & I.ToString
  11.             For J As Integer = 0 To 2
  12.                 With ActiveRO(I).ROStories(J)
  13.                     .StoryID = "StoryID" & J.ToString
  14.                     .StorySlug = "Slug" & J.ToString
  15.                     .StoryBody = "Now is the time for all good men to come to the aid of their country."
  16.                 End With
  17.             Next J
  18.         End With
  19.  
  20.         UpdateROInfo(ActiveRO)
  21.  
  22.     End Sub
  23. End Class
  24.  
The error I get is in the UpdateROInfo function on the xs.Serialize(fs, AllROs) line. The errors (there are two) are:

InnerException: When casting from a number, the value must be a number less than infinity.

InnerException: Make sure the source type is convertible to the destination type.
Feb 27 '10 #1

✓ answered by !NoItAll

Go here for my write-up on Serialization (Serialisation for everywhere else except America)!

http://bytes.com/topic/visual-basic-...et#post3549581

2 2365
!NoItAll
297 100+
Not sure if this will solve my problem, but I spent the weekend teaching myself some of the basics of serialization. I think there are just so many problems with the code above that deconstructing it would be arduous - so it's not surprising nobody has jumped in to rescue me.
Because I feel strongly that no thread should go un-answered in some way (an unanswered thread, in my opinion, should be deleted after 30 days - otherwise it just adds to the already poor signal to noise ratio on the net).
As a learning tool I put together a little project. I'm going to put it into the insights section for anyone who wants to learn the basics of serialization. Believe me - it's worth it. Trying to hand-roll xml by creating an XML Document Object and setting all of the various element and attribute properties manually is code that will simply collapse under its own weight. Serialization to XML is the answer. Look for the article in the insights section soon...
Feb 28 '10 #2
!NoItAll
297 100+
Go here for my write-up on Serialization (Serialisation for everywhere else except America)!

http://bytes.com/topic/visual-basic-...et#post3549581
Feb 28 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Aaron Clamage | last post by:
Hi, I'm not sure that if this is the right forum, but any help would be greatly appreciated. I am porting some java serialization code to c# and I can't figure out the correct way to do it. ...
3
by: Noël Danjou | last post by:
VS.NET 2003, C# and .NET Framework 1.1. Hello, I am trying to serialize a small sample class using the XmlSerializer class. ** Here is the code: DerivedClass myClass = new DerivedClass(5,...
5
by: Tamir Khason | last post by:
I have an object (eg MyObject with namespace eg MyNamespace) public class MyObject While I serialize it the XML created is <MyObject xmlns:xsd="http://www.w3.org/2001/XMLSchema"...
5
by: francois | last post by:
First of all I would to to apologize for resending this post again but I feel like my last post as been spoiled Here I go for my problem: Hi, I have a webservice that I am using and I would...
6
by: Uttam | last post by:
Hello, We are at a very crucial decision making stage to select between .Net and Java. Our requirement is to download a class at runtime on the client computer and execute it using remoting or...
2
by: Maximus | last post by:
Hi Everyone, I was using Inprocess session objects, but incase of aspnet process crashes the session objects were lost as a result I decided to shift to out of porocess session objects. For this...
2
by: jakk | last post by:
Below is the exception that Iam getting. It says that the DataView that Iam storing in the session is not Serializable. BUt works fine if I store in the inproc session and fails if I switch to...
2
by: Norman Chong | last post by:
Hiddeldi ho, I want to save an object so that I can use its content after I restart my program. I tried to solve this with serialization because someone told me that this is the correct way for...
8
by: =?Utf-8?B?UGlnZ3k=?= | last post by:
Hi to all, I am getting this System.OutOfMemoryException calling the Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(<stream>,<Obj>) method. The type of <streamis...
2
by: mkvenkit.vc | last post by:
Hello, I hope this is the right place to post a question on Boost. If not, please let me know where I can post this message and I will do so. I am having a strange problem with std::string as...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.