简体   繁体   English

如何将 Microsoft xsd.exe 与 TrainingCenterDatabasev2 架构一起使用?

[英]How to use Microsoft xsd.exe with TrainingCenterDatabasev2 Schema?

I have TCX exercise files which are written using the schema athttps://www8.garmin.com/xmlschemas/TrainingCenterDatabasev2.xsd .我有使用https://www8.garmin.com/xmlschemas/TrainingCenterDatabasev2.xsd 上的模式编写的 TCX 练习文件。 I have been using them for years with Java and JAXB.我多年来一直在 Java 和 JAXB 中使用它们。 I am trying to write a C# application to do the same thing.我正在尝试编写一个 C# 应用程序来做同样的事情。 It is not going well.进展不顺利。 I can generate C# classes using xsd.exe as provided by Visual Studio.我可以使用 Visual Studio 提供的 xsd.exe 生成 C# 类。 However, they do not make sense to me and cannot be used to deserialize my TCX files.但是,它们对我没有意义,不能用于反序列化我的 TCX 文件。

The basic structure of TCX files (at least the part in which I am interested) is they have a number of Activities containing a number of Laps containing a number of Tracks containing a number of Trackpoints. TCX 文件的基本结构(至少是我感兴趣的部分)是它们有许多活动,其中包含许多圈数,其中包含许多包含许多轨迹点的轨迹。 The Trackpoints have latitude, longitude, and heart rate as the main items of interest. Trackpoints 以纬度、经度和心率为主要关注项。

The xsd-generated C# classes have an Activity_t[], an ActivityLap_t[], and a Trackpoint_t[][]. xsd 生成的 C# 类有一个 Activity_t[]​​、一个 ActivityLap_t[] 和一个 Trackpoint_t[][]。 There is no Track_t[] and the string Track_t does not appear in the file even though it is in the .xsd, for example in this excerpt for the Lap and Track.没有 Track_t[] 并且字符串 Track_t 没有出现在文件中,即使它在 .xsd 中,例如在这个圈和赛道的摘录中。

<xsd:complexType name="ActivityLap_t">
        <xsd:sequence>
            <xsd:element name="TotalTimeSeconds" type="xsd:double"/>
            <xsd:element name="DistanceMeters" type="xsd:double"/>
            <xsd:element name="MaximumSpeed" type="xsd:double" minOccurs="0"/>
            <xsd:element name="Calories" type="xsd:unsignedShort"/>
            <xsd:element name="AverageHeartRateBpm" type="HeartRateInBeatsPerMinute_t" minOccurs="0"/>
            <xsd:element name="MaximumHeartRateBpm" type="HeartRateInBeatsPerMinute_t" minOccurs="0"/>
            <xsd:element name="Intensity" type="Intensity_t"/>
            <xsd:element name="Cadence" type="CadenceValue_t" minOccurs="0"/>
            <xsd:element name="TriggerMethod" type="TriggerMethod_t"/>
            <xsd:element name="Track" type="Track_t" minOccurs="0" maxOccurs="unbounded"/>
            <xsd:element name="Notes" type="xsd:string" minOccurs="0"/>
            <xsd:element name="Extensions" type="Extensions_t" minOccurs="0">
                <xsd:annotation>
                    <xsd:documentation>You can extend Training Center by adding your own elements from another schema here.</xsd:documentation>
                </xsd:annotation>
            </xsd:element>
        </xsd:sequence>
        <xsd:attribute name="StartTime" type="xsd:dateTime" use="required"/>
    </xsd:complexType>

    <xsd:complexType name="Track_t">
        <xsd:sequence>
            <xsd:element name="Trackpoint" type="Trackpoint_t" maxOccurs="unbounded"/>
        </xsd:sequence>
    </xsd:complexType>

I don't understand how to deal with the [][] nor why it would be generated.我不明白如何处理 [][] 也不明白为什么会生成它。 And as mentioned it fails to parse.如前所述,它无法解析。

error CS0030: Cannot convert type 'TrainingCenterDatabaseV2.Trackpoint_t[] to TrainingCenterDatabaseV2.Trackpoint_t.错误 CS0030:无法将类型“TrainingCenterDatabaseV2.Trackpoint_t[] 转换为 TrainingCenterDatabaseV2.Trackpoint_t。

(The namespace I used is TrainingCenterDatabaseV2.) (我使用的命名空间是 TrainingCenterDatabaseV2。)

This is the code used to deserialize:这是用于反序列化的代码:

private const string NS_TrainingCenterDatabase_v2 = "http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2";

XmlSerializer xmlSerializer = new XmlSerializer(typeof(TrainingCenterDatabase_t),
                NS_TrainingCenterDatabase_v2);
FileStream fs = new FileStream(fileName, FileMode.Open);
XmlReader reader = XmlReader.Create(fs);
TrainingCenterDatabase_t tcx = (TrainingCenterDatabase_t)xmlSerializer.Deserialize(reader);

Not knowing where to go with this I tried the Visual Studio plugin, xsd2code++.不知道该去哪里,我尝试了 Visual Studio 插件 xsd2code++。 This generates sensible C# classes with List<Activity_t>, List<ActivityLap_t>, and List<Trackpoint_t>.这将生成具有 List<Activity_t>、List<ActivityLap_t> 和 List<Trackpoint_t> 的合理 C# 类。 It also does not have Track_t, which seems to be somewhat superfluous.它也没有Track_t,这似乎有些多余。

However, it also fails to parse.但是,它也无法解析。 I believe the problems here have to do with the lack of annotations it generates, compared to the ones generated from xsd.exe.我认为,与 xsd.exe 生成的注释相比,这里的问题与它生成的注释缺乏有关。 I believe the problem is that it cannot handle the namespaces without those annotations, but I have seen no way to set options to get around that.我相信问题在于它无法处理没有这些注释的命名空间,但我没有看到设置选项来解决这个问题。 As stated, I am not experienced with deserialization in C#.如前所述,我对 C# 中的反序列化没有经验。 For my current purposes I have implemented reading the TCX files using XDocument rather than deserialization.对于我目前的目的,我使用 XDocument 而不是反序列化来实现读取 TCX 文件。 However, I am curious as to why what I tried did not work, especially since xsd.exe has been around for a long time.但是,我很好奇为什么我尝试的方法不起作用,尤其是因为 xsd.exe 已经存在很长时间了。

Thanks in advance.提前致谢。

This appears to be a bug with the xsd.exe tool itself.这似乎是xsd.exe工具本身的错误。 I would recommend using LinqToXsd (requires .NET Core 2.1), which is another Microsoft-developed technology for accessing XML data using an XSD;我建议使用LinqToXsd (需要 .NET Core 2.1),这是 Microsoft 开发的另一种使用 XSD 访问 XML 数据的技术; it's also more advanced than xsd.exe and in my quick testing appears to fully handle the above Garmin training center database schema without issue.它也比xsd.exe更先进,并且在我的快速测试中似乎可以完全处理上述 Garmin 培训中心数据库架构而没有问题。

Also if you cannot install .NET Core on your machine, you can use this nuget package instead.此外,如果您无法在您的机器上安装 .NET Core,您可以改用这个nuget 包 The .NET Core version requires .NET Core 2.1 to actually generate code, but that generated code that can be used in an app that targets .NET Framework 4.6.2 and above. .NET Core 版本需要 .NET Core 2.1 才能实际生成代码,但生成的代码可用于面向 .NET Framework 4.6.2 及更高版本的应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM