Serialization in CF problem

  • Thread starter Thread starter jamie
  • Start date Start date
J

jamie

OK I'm not sure how explain this without giving hundreds of lines of code.

basically I have a main class that I want to serialize. It's constructor
creates more classes that I also want serialized.


public Mainclass{

public subclass1 class1;

public MainClass()
{
class1 = new subclass1()
}
}

//Now that subclass wants to create more classes

public subclass1 {

public string blah;
public subclass2[] class2array;


public subclass1()
{
blah = "blah";
class2array = new class2array[2]

for (int i =0; i<2; i++)
{
class2array = new subclass2(i.ToString());
}

}
}

//which creates

public subclass2{

public string name;

public subclass2(string inName)
{
name = in Name;
}

public subclass2()
{
// default constructor
}
}

Now when I serialize this thing I'd hope I get back something like

<?xml version="1.0" encoding="utf-8"?>
<MainClass xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001...>
<subClass1>
<blah>blah</blah>
<subclass2>
<name>0</name>
</subclass2>
<subclass2>
<name>1</name>
</subclass2>
</subClass1>
</MainClass >

Running this in the regular framework I do get that back However running
this in the compact framework I get back

<MainClass xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001...>
<subClass1>
<blah>blah</blah>
</subClass1>
</MainClass >

The difference seems to be coming from within the loops but I can't see what
Is going wrong there. Is this a known thing? I didn't post my actual code
since it's miles long but this is the general idea of what is failing. If
I throw in a debug message in the constructor for subClass2 It does fire
so it is getting all the way down there it's just not showing up in the XML
at the end.

Any idea?

sorry for the cross post but the XML guys seem to not answer many CF
questions.
 
This is not what I see. I compiled your code (fixed a couple of typos) and
got the following xml:

<?xml version="1.0"?>
<MainClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<class1>
<class2array>
<subclass2>
<name>0</name>
</subclass2>
<subclass2>
<name>1</name>
</subclass2>
</class2array>
<blah>blah</blah>
</class1>
</MainClass>"


The serialization code looked like this:
XmlSerializer xs = new XmlSerializer(typeof(MainClass));
MemoryStream ms = new MemoryStream();
xs.Serialize(ms, new MainClass());
byte[] data = ms.ToArray();
Debug.WriteLine(Encoding.ASCII.GetString(data, 0, data.Length));
 
Hmm I never actually tried running the sample code that I cooked up for an
example. What this suggests is there is actually a difference between my
CF program and the PC version. I really wish it would just die on an error
or something.

Ok thanks I'll keep checking the two to see if I can find anything.

Alex Feinman said:
This is not what I see. I compiled your code (fixed a couple of typos) and
got the following xml:

<?xml version="1.0"?>
<MainClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<class1>
<class2array>
<subclass2>
<name>0</name>
</subclass2>
<subclass2>
<name>1</name>
</subclass2>
</class2array>
<blah>blah</blah>
</class1>
</MainClass>"


The serialization code looked like this:
XmlSerializer xs = new XmlSerializer(typeof(MainClass));
MemoryStream ms = new MemoryStream();
xs.Serialize(ms, new MainClass());
byte[] data = ms.ToArray();
Debug.WriteLine(Encoding.ASCII.GetString(data, 0,
data.Length));



jamie said:
OK I'm not sure how explain this without giving hundreds of lines of
code.

basically I have a main class that I want to serialize. It's
constructor creates more classes that I also want serialized.


public Mainclass{

public subclass1 class1;

public MainClass()
{
class1 = new subclass1()
}
}

//Now that subclass wants to create more classes

public subclass1 {

public string blah;
public subclass2[] class2array;


public subclass1()
{
blah = "blah";
class2array = new class2array[2]

for (int i =0; i<2; i++)
{
class2array = new subclass2(i.ToString());
}

}
}

//which creates

public subclass2{

public string name;

public subclass2(string inName)
{
name = in Name;
}

public subclass2()
{
// default constructor
}
}

Now when I serialize this thing I'd hope I get back something like

<?xml version="1.0" encoding="utf-8"?>
<MainClass xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001...>
<subClass1>
<blah>blah</blah>
<subclass2>
<name>0</name>
</subclass2>
<subclass2>
<name>1</name>
</subclass2>
</subClass1>
</MainClass >

Running this in the regular framework I do get that back However running
this in the compact framework I get back

<MainClass xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001...>
<subClass1>
<blah>blah</blah>
</subClass1>
</MainClass >

The difference seems to be coming from within the loops but I can't see
what Is going wrong there. Is this a known thing? I didn't post my
actual code since it's miles long but this is the general idea of what is
failing. If I throw in a debug message in the constructor for subClass2
It does fire so it is getting all the way down there it's just not
showing up in the XML at the end.

Any idea?

sorry for the cross post but the XML guys seem to not answer many CF
questions.

 
Back
Top