热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

尝试对从复杂XSD生成的类进行序列化时出现NullReferenceException错误

在尝试对从复杂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 个解决方案

#1


1  

Line trackPoint[0].Time = DateTime.Now; gives the mentioned error

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

Look at your code, you initialize the aforementioned array here:

看看你的代码,你在这里初始化上述数组:

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

but you never initialize the Trackpoint_t ITSELF in the array, which causes this

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

trackPoint[0].Time = DateTime.Now; 

to fail (trackPoint[0] is still a null)

失败(trackPoint [0]仍为null)


推荐阅读
  • 深入理解Tornado模板系统
    本文详细介绍了Tornado框架中模板系统的使用方法。Tornado自带的轻量级、高效且灵活的模板语言位于tornado.template模块,支持嵌入Python代码片段,帮助开发者快速构建动态网页。 ... [详细]
  • 本文介绍了如何使用JQuery实现省市二级联动和表单验证。首先,通过change事件监听用户选择的省份,并动态加载对应的城市列表。其次,详细讲解了使用Validation插件进行表单验证的方法,包括内置规则、自定义规则及实时验证功能。 ... [详细]
  • 从 .NET 转 Java 的自学之路:IO 流基础篇
    本文详细介绍了 Java 中的 IO 流,包括字节流和字符流的基本概念及其操作方式。探讨了如何处理不同类型的文件数据,并结合编码机制确保字符数据的正确读写。同时,文中还涵盖了装饰设计模式的应用,以及多种常见的 IO 操作实例。 ... [详细]
  • Explore how Matterverse is redefining the metaverse experience, creating immersive and meaningful virtual environments that foster genuine connections and economic opportunities. ... [详细]
  • PyCharm下载与安装指南
    本文详细介绍如何从官方渠道下载并安装PyCharm集成开发环境(IDE),涵盖Windows、macOS和Linux系统,同时提供详细的安装步骤及配置建议。 ... [详细]
  • 本文详细介绍了如何使用 Yii2 的 GridView 组件在列表页面实现数据的直接编辑功能。通过具体的代码示例和步骤,帮助开发者快速掌握这一实用技巧。 ... [详细]
  • Python自动化处理:从Word文档提取内容并生成带水印的PDF
    本文介绍如何利用Python实现从特定网站下载Word文档,去除水印并添加自定义水印,最终将文档转换为PDF格式。该方法适用于批量处理和自动化需求。 ... [详细]
  • XNA 3.0 游戏编程:从 XML 文件加载数据
    本文介绍如何在 XNA 3.0 游戏项目中从 XML 文件加载数据。我们将探讨如何将 XML 数据序列化为二进制文件,并通过内容管道加载到游戏中。此外,还会涉及自定义类型读取器和写入器的实现。 ... [详细]
  • 本文详细探讨了在Android 8.0设备上使用ChinaCock的TCCBarcodeScanner进行扫码时出现的应用闪退问题,并提供了解决方案。通过调整配置文件,可以有效避免这一问题。 ... [详细]
  • 本文详细解析了Python中的os和sys模块,介绍了它们的功能、常用方法及其在实际编程中的应用。 ... [详细]
  • 本文讨论了如何根据特定条件动态显示或隐藏文件上传控件中的默认文本(如“未选择文件”)。通过结合CSS和JavaScript,可以实现更灵活的用户界面。 ... [详细]
  • 本文深入探讨了 Java 编程语言的基础,特别是其跨平台特性和 JVM 的工作原理。通过介绍 Java 的发展历史和生态系统,帮助初学者理解如何编写并运行第一个 Java 程序。 ... [详细]
  • Hadoop入门与核心组件详解
    本文详细介绍了Hadoop的基础知识及其核心组件,包括HDFS、MapReduce和YARN。通过本文,读者可以全面了解Hadoop的生态系统及应用场景。 ... [详细]
  • 毕业设计:基于机器学习与深度学习的垃圾邮件(短信)分类算法实现
    本文详细介绍了如何使用机器学习和深度学习技术对垃圾邮件和短信进行分类。内容涵盖从数据集介绍、预处理、特征提取到模型训练与评估的完整流程,并提供了具体的代码示例和实验结果。 ... [详细]
  • 本文介绍如何在现有网络中部署基于Linux系统的透明防火墙(网桥模式),以实现灵活的时间段控制、流量限制等功能。通过详细的步骤和配置说明,确保内部网络的安全性和稳定性。 ... [详细]
author-avatar
111
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有