作者:111 | 来源:互联网 | 2024-11-02 17:25
在尝试对从复杂XSD生成的类进行序列化时,遇到了`NullReferenceException`错误。尽管已经花费了数小时进行调试和搜索相关资料,但仍然无法找到问题的根源。希望社区能够提供一些指导和建议,帮助解决这一难题。
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.
一直在挠我的头几个小时。用Google搜索。但无法弄清楚如何正确生成xml。非常感谢能够帮助我解决问题的输入。我之前使用过xsd.exe和不太复杂的方案,没有任何问题。
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.xsd
我从这个xsd文件创建了C#类:http://www8.garmin.com/xmlschemas/TrainingCenterDatabasev2.xsd
I created the classes with the Microsoft xsd.exe tool like this: xsd.exe xsd-file /classes
我使用Microsoft xsd.exe工具创建了类,如下所示:xsd.exe xsd-file / classes
Then 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-Files
我实际上不知道使用从xsd-document生成的类创建xml文件的正确方法。以下是此类xml文件的示例:https://github.com/mlt/schwinn810/wiki/Sample-.TCX-Files
This is my object that I'm trying to serialize (just an example):
这是我的目标,我正在尝试序列化(只是一个例子):
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; 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.
Line trackPoint [0] .Time = DateTime.Now;给出了上述错误。但我认为与xsd / xml的外观相比,它与创建类/ xml的错误更相关。
Could someone just point me in the right direction concerning how to build up the xml from the class generated by xsd.exe?
有人可以指出我正确的方向,关于如何从xsd.exe生成的类建立xml?
Edit: Thanks YavgenyP! That was it, this code is working:
编辑:谢谢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 个解决方案