hi, i am trying to serialize the following class :
namespace MyNameSpace
{
[System.Xml.Serialization.XmlRootAttribute("Station Data", Namespace="",
IsNullable=false)]
public class StationData
{
[System.Xml.Serialization.XmlElementAttribute("Stat ion")]
public Station[] Station;
}
public class Station
{
public int ID;
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool IDSpecified;
public string Name;
[System.Xml.Serialization.XmlElementAttribute("Game Sets")]
public GameSetData[] GameSets;
}
public class GameSetData
{
public int ID;
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool IDSpecified;
public string Name;
}
}
to the following XML:
<?xml version="1.0" encoding="utf-8" ?>
<StationData>
<Station>
<ID>-1</ID>
<Name>ALL</Name>
<GameSets>
<ID>0</ID>
<Name>x</Name>
</GameSets>
<GameSets>
<ID>2</ID>
<Name>xx</Name>
</GameSets>
<GameSets>
<ID>4</ID>
<Name>xxx</Name>
</GameSets>
<GameSets>
<ID>6</ID>
<Name>xxxx</Name>
</GameSets>
<GameSets>
<ID>8</ID>
<Name>xxxxx</Name>
</GameSets>
</Station>
<Station>
<ID>1</ID>
<Name>xxxxxxx</Name>
<GameSets>
<ID>6</ID>
<Name>xxxx</Name>
</GameSets>
</Station>
<Station>
<ID>2</ID>
<Name>xxxxxxxx</Name>
<GameSets>
<id>4</id>
<Name>xxx</Name>
</GameSets>
<GameSets>
<id>0</id>
<Name>x</Name>
</GameSets>
</Station>
<Station>
<ID>3</ID>
<Name>xxxxxxxxxx</Name>
<GameSets>
<ID>2</ID>
<Name>xx</Name>
</GameSets>
<GameSets>
<ID>8</ID>
<Name>xxxxx</Name>
</GameSets>
</Station>
</StationData>
with the following code:
ArrayList newStations = new ArrayList();
for(int j=0; j< m_StationData.Station.Length; j++)
{
// do some stuff here
}
Station[] NewStations = newStations.ToArray(typeof(Station)) as
Station[];
// write stationdata to XML file
string fileName = @"c:/logs/swmstations_new.xml";
XmlSerializer serializer = new XmlSerializer(typeof(Station),
"StationData");
TextWriter textwriter = new StreamWriter(fileName);
serializer.Serialize(textwriter, NewStations);
textwriter.Close();
it throws an inner exception of "the specified cast is not valid" - i
have tried various other permutations of the new XMLSerializer() and
they all throw an exception.
mind you, the xml document deserializes into the class without a
problem. xsd.exe generated the class file for me.
am i missing something here? or, is there an easy way to cast my
Station[] array as a stationdata object that will write to the xml
cleanly?