从复杂的xsd生成的类,尝试序列化给出nullreferenceexception

时间:2012-05-25 21:19:50

标签: c# xml xsd

一直在挠头几个小时。用Google搜索。但无法弄清楚如何正确生成xml。非常感谢能够帮助我解决这个问题的输入。我之前使用过xsd.exe和不太复杂的方案,没有任何问题。

所以我得到错误:对象引用未设置为对象的实例。

我已经从这个xsd文件中创建了C#Classes:http://www8.garmin.com/xmlschemas/TrainingCenterDatabasev2.xsd

我使用Microsoft xsd.exe工具创建了这样的类:xsd.exe xsd-file / classes

然后我删除了像[] []这样的双括号,并替换为单个[],否则我根本无法序列化/反序列化。

我实际上并不知道使用xsd-document生成的类创建xml文件的正确方法。以下是此类xml文件的示例:https://github.com/mlt/schwinn810/wiki/Sample-.TCX-Files

我的目标是我尝试序列化(只是一个例子):

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;

Line trackPoint [0] .Time = DateTime.Now;给出了上述错误。但我认为与xsd / xml的外观相比,它与创建类/ xml的错误更相关。

有人可以指出我正确的方向,关于如何从xsd.exe生成的类中构建xml?

编辑:谢谢YavgenyP!就是这样,这段代码正在运行:

        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;

1 个答案:

答案 0 :(得分:1)

  

Line trackPoint [0] .Time = DateTime.Now;给出了上述错误

查看代码,在此处初始化上述数组:

XmlObjects.Tcx20.Trackpoint_t[] trackPoint = new Trackpoint_t[1];

但是你永远不会在数组中初始化Trackpoint_t ITSELF,这会导致这个

trackPoint[0].Time = DateTime.Now; 

失败(trackPoint [0]仍为空)