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

ArrayList help

I'm really stumped on this. Serializing an ArrayList to XML works fine when
i use built in classes but when i try storing my own classes in ArrayLists
and serializing them the CLR throwns an exception. Can someone give me a
clue as to what's going on?

using System;
using System.Collections;

namespace tApp
{
public class TestClass
{
public string myStr;

public TestClass(string s)
{
myStr = s;
}
}

public class Class1
{
public ArrayList arr;

public Class1()
{
arr = new ArrayList();

for(int i=0; i<5; i++)
{
arr.Add(new TestClass((string)"Line #" + i.ToString()));
}
}
}
}

Nov 15 '05 #1
2 1539
You didn't provide a complete program, nor the exception, but I cobbled
together an example using your code that works:

using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.Collections;

namespace tApp
{
public class TestClass
{
public string myStr;

public TestClass() {}

public TestClass(string s)
{
myStr = s;
}
}

public class Class1
{
public static void Main()
{
ArrayList arr = new ArrayList();

for(int i=0; i<5; i++)
{
arr.Add(new TestClass((string)"Line #" + i.ToString()));
}

XmlSerializer xser = new XmlSerializer(typeof(ArrayList), new Type[]
{typeof(TestClass)});
MemoryStream stream = new MemoryStream();
XmlWriter writer = new XmlTextWriter(stream, System.Text.Encoding.ASCII);

xser.Serialize(writer, arr);

}
}
}
--
C#, .NET and Complex Adaptive Systems:
http://blogs.geekdojo.net/Richard
"Chuck Bowling" <ch**********@sbcglobal-NO-SPAM.net> wrote in message
news:O2**************@TK2MSFTNGP12.phx.gbl...
I'm really stumped on this. Serializing an ArrayList to XML works fine when i use built in classes but when i try storing my own classes in ArrayLists
and serializing them the CLR throwns an exception. Can someone give me a
clue as to what's going on?

using System;
using System.Collections;

namespace tApp
{
public class TestClass
{
public string myStr;

public TestClass(string s)
{
myStr = s;
}
}

public class Class1
{
public ArrayList arr;

public Class1()
{
arr = new ArrayList();

for(int i=0; i<5; i++)
{
arr.Add(new TestClass((string)"Line #" + i.ToString()));
}
}
}
}

Nov 15 '05 #2
Ah, thanks Richard. Sorry about not posting the serialization routine. That
was the problem. Bad arguments XmlSerializer.
"Richard A. Lowe" <ch*****@yumspamyumYahoo.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
You didn't provide a complete program, nor the exception, but I cobbled
together an example using your code that works:

using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.Collections;

namespace tApp
{
public class TestClass
{
public string myStr;

public TestClass() {}

public TestClass(string s)
{
myStr = s;
}
}

public class Class1
{
public static void Main()
{
ArrayList arr = new ArrayList();

for(int i=0; i<5; i++)
{
arr.Add(new TestClass((string)"Line #" + i.ToString()));
}

XmlSerializer xser = new XmlSerializer(typeof(ArrayList), new Type[]
{typeof(TestClass)});
MemoryStream stream = new MemoryStream();
XmlWriter writer = new XmlTextWriter(stream, System.Text.Encoding.ASCII);
xser.Serialize(writer, arr);

}
}
}
--
C#, .NET and Complex Adaptive Systems:
http://blogs.geekdojo.net/Richard
"Chuck Bowling" <ch**********@sbcglobal-NO-SPAM.net> wrote in message
news:O2**************@TK2MSFTNGP12.phx.gbl...
I'm really stumped on this. Serializing an ArrayList to XML works fine

when
i use built in classes but when i try storing my own classes in ArrayLists and serializing them the CLR throwns an exception. Can someone give me a
clue as to what's going on?

using System;
using System.Collections;

namespace tApp
{
public class TestClass
{
public string myStr;

public TestClass(string s)
{
myStr = s;
}
}

public class Class1
{
public ArrayList arr;

public Class1()
{
arr = new ArrayList();

for(int i=0; i<5; i++)
{
arr.Add(new TestClass((string)"Line #" + i.ToString()));
}
}
}
}


Nov 15 '05 #3

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

Similar topics

4
by: Stephen | last post by:
I have got an event below to remove items from an arraylist and then to rebind the arraylist to the datagrid subsequently deleting the appropriate row. My problem is that my code makes sense and I...
3
by: Stephen | last post by:
I've been working on some code which will hopefully allow me to transfer the contents of an arraylist on one page over to another page. The Arraylist is held in the viewstate object as it is...
2
by: Stephen | last post by:
I am trying to delete a row in a datagrid on the onclick of a asp:ButtonColumn. The datagrid is created from the items in an arraylist so what im trying to do is remove the item from the array and...
3
by: Stephen | last post by:
I was wondering if someone can help me with an web application design problem. I have a aspx page which builds up an arraylist called addresses and outputs the values in the arraylist items to a...
9
by: Leon | last post by:
I have a webform in which when the user press generate button the form generate six unique ramdon numbers, the user can also type these six numbers in manually in any order. however, the user can...
7
by: Dave | last post by:
Hi all, I have... using System.Collections; namespace MyApp.Templates { public class MyPage : System.Web.UI.Page { ArrayList MyArray = new ArrayList();
6
by: gane kol | last post by:
Hi, I have a code that creates a datatable from an arraylist, but i am getting an error in casting in for (int intRow = 0; intRow < alLCPlist.Count; intRow++) { DataRow drow =...
6
by: GrandpaB | last post by:
While writing this plea for help, I think I solved my dilemma, but I don't know why the problem solving statement is necessary. The inspiration for the statement came from an undocumented VB...
18
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the...
6
by: fniles | last post by:
I am using VB.NET 2003 and a socket control to receive and sending data to clients. As I receive data in 1 thread, I put it into an arraylist, and then I remove the data from arraylist and send it...
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
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
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,...
0
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...
0
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...

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.