Yes, this is nice!
However, one has to know in advance the type of the elements of the array
and this is one of the not-answered questions by the OP.
I changed slightly your example and now it throws an exception.
In the code below I have an array of Object. The first element of the array
is a Fred instance, the rest are ints:
using System;
namespace Ionic
{
public struct Fred
{
public int A;
public string B;
public bool C;
}
class Driver
{
static void Main(string[] args)
{
System.Random rnd= new System.Random();
int n= rnd.Next(3)+4;
// the array
Object[] f= new Object[n];
Fred f1 = new Fred();
f[0]= f1;
f1.A= rnd.Next(17);
f1.B= new System.String((char)(rnd.Next(26)+65), rnd.Next(20)+10);
f1.C= ((rnd.Next(10) % 2) == 0);
// fill with junk data
for ( int i=1; i < n; i++)
{
f[i]= i;
}
//Serialize
try
{
System.Xml.Serialization.XmlSerializer s = new
System.Xml.Serialization.XmlSerializer(typeof(Obje ct[]));
s.Serialize(System.Console.Out,f);
System.Console.WriteLine();
}
catch(Exception e)
{
string s = e.Message;
System.Console.WriteLine(s);
}
}
}
}
What am I doing wrong?
=====
Cheers,
Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL
"Dino Chiesa [Microsoft]" <di****@online.microsoft.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
namespace Ionic {
public struct Fred {
public int A;
public string B;
public bool C;
}
class Driver {
static void Main(string[] args) {
System.Random rnd= new System.Random();
int n= rnd.Next(3)+4;
// the array
Fred[] f= new Fred[n];
// fill with junk data
for ( int i=0; i < n; i++) {
f[i]= new Fred();
f[i].A= rnd.Next(17);
f[i].B= new System.String((char)(rnd.Next(26)+65),
rnd.Next(20)+10); f[i].C= ((rnd.Next(10) % 2) == 0);
}
//Serialize
System.Xml.Serialization.XmlSerializer s = new
System.Xml.Serialization.XmlSerializer(typeof(Fred[]));
s.Serialize(System.Console.Out,f);
System.Console.WriteLine();
}
}
}
"Dimitre Novatchev" <dn********@yahoo.com> wrote in message
news:br************@ID-152440.news.uni-berlin.de...
"Abraham Lopez" <ab***********@hotmail.com> wrote in message
news:O6**************@TK2MSFTNGP12.phx.gbl... Hi.. Is there a way to convert a System.Array to XML...
If you know thanks very much...
if you don't... Please do not respond stupid things like " Yes -- many
ways."
The answer you got only reflects the question... :o)
Your question is so general, that it belongs to the same group as "Is
there a way to work with a computer"
Collin already pointed out some certain properties of your question (to
state it in a more neutral way, its lack of concreteness makes it not
too meaningful).
To the questions that Collin asked in order to make sense of your
question, I would also add the following:
Are you asking is it possible to represent *the object* System.Array
as an XML document or do you have in mind a particular instance of
System.Array?
What is the type of data contained in the array elements -- e.g.
simple values or other objects?
What names should be used for the elements of this xml document?
What should be the encoding used ?
Or maybe you want to embed the source code representation of the
array simply as a text node?
=====
Cheers,
Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL