ArrayList help

C

Chuck Bowling

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()));
}
}
}
}
 
R

Richard A. Lowe

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

Chuck Bowling

Ah, thanks Richard. Sorry about not posting the serialization routine. That
was the problem. Bad arguments XmlSerializer.


Richard A. Lowe said:
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 said:
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()));
}
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top