0

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

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

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

我使用 Microsoft xsd.exe 工具创建了这些类,如下所示:xsd.exe xsd-file /classes

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

我实际上不知道使用从 xsd 文档生成的类创建 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;

线 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;
4

1 回答 1

1

线 trackPoint[0].Time = DateTime.Now; 给出提到的错误

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

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

但是您从未在数组中初始化 Trackpoint_t ITSELF,这会导致

trackPoint[0].Time = DateTime.Now; 

失败(trackPoint[0] 仍然为空)

于 2012-05-26T09:30:17.703 回答