Class generated from complex xsd, trying to serialize gives nullreferenceexception

vibramzya9

New Member
Been scratching my head for some hours. Googled as well. But can't figure out how to generate the xml properly. Would highly appreciate input that could help me figure this out. I've used xsd.exe earlier together with less complex schemes without any problems.So I get the error: Object reference not set to an instance of an object.I have created C# Classes from this xsd-file: http://www8.garmin.com/xmlschemas/TrainingCenterDatabasev2.xsdI created the classes with the Microsoft xsd.exe tool like this: xsd.exe xsd-file /classesThen I removed double brackets like [][] and replaced with single [], otherwise I cant serialize/deserialize at all.I actually don't know the correct way to create a xml-file with the class generated from the xsd-document. Here is an example of such a xml-file: https://github.com/mlt/schwinn810/wiki/Sample-.TCX-FilesThis is my object that I'm trying to serialize (just an example):\[code\]XmlObjects.Tcx20.TrainingCenterDatabase_t tcx = new XmlObjects.Tcx20.TrainingCenterDatabase_t();XmlObjects.Tcx20.AbstractSource_t abstractSource = new XmlObjects.Tcx20.Application_t();abstractSource.Name = "TcxCreator";tcx.Author = abstractSource;abstractSource = new XmlObjects.Tcx20.Application_t();XmlObjects.Tcx20.ActivityList_t activityList = new XmlObjects.Tcx20.ActivityList_t();XmlObjects.Tcx20.Activity_t[] activity = new XmlObjects.Tcx20.Activity_t[1];XmlObjects.Tcx20.ActivityLap_t[] lap = new ActivityLap_t[1];XmlObjects.Tcx20.Course_t[] course = new Course_t[1];XmlObjects.Tcx20.Trackpoint_t[] trackPoint = new Trackpoint_t[1];XmlObjects.Tcx20.Position_t position = new Position_t();double lat = 10;double lon = 11;position.LatitudeDegrees = lat;position.LongitudeDegrees = lon;trackPoint[0].Time = DateTime.Now;trackPoint[0].Position = position;lap[0].Track = trackPoint;activity[0].Lap = lap;activityList.Activity = activity;tcx.Activities = activityList;\[/code\]Line trackPoint[0].Time = DateTime.Now; gives the mentioned error. But i think its more related to that im creating the classes/xml wrong compared to how the xsd/xml looks like. Could someone just point me in the right direction concerning how to build up the xml from the class generated by xsd.exe?Edit: Thanks YavgenyP! That was it, this code is working:\[code\] XmlObjects.Tcx20.TrainingCenterDatabase_t tcx = new XmlObjects.Tcx20.TrainingCenterDatabase_t(); XmlObjects.Tcx20.AbstractSource_t abstractSource = new XmlObjects.Tcx20.Application_t(); abstractSource.Name = "TcxCreator"; tcx.Author = abstractSource; abstractSource = new XmlObjects.Tcx20.Application_t(); XmlObjects.Tcx20.ActivityList_t activityList = new XmlObjects.Tcx20.ActivityList_t(); XmlObjects.Tcx20.Activity_t[] activity = new XmlObjects.Tcx20.Activity_t[1]; XmlObjects.Tcx20.ActivityLap_t[] lap = new ActivityLap_t[1]; XmlObjects.Tcx20.Course_t[] course = new Course_t[1]; XmlObjects.Tcx20.Trackpoint_t[] trackPoint = new Trackpoint_t[1]; XmlObjects.Tcx20.Position_t position = new Position_t(); double lat = 10; double lon = 11; position.LatitudeDegrees = lat; position.LongitudeDegrees = lon; trackPoint[0] = new Trackpoint_t {Time = DateTime.Now, Position = position}; lap[0] = new ActivityLap_t {Track = trackPoint}; activity[0] = new Activity_t {Lap = lap}; activityList.Activity = activity; tcx.Activities = activityList;\[/code\]
 
Back
Top