用于生成C#代码的XML语法 - java

我目前正在尝试将Java库的一部分转换为C#库,以便在Unity中使用。在此Java库中,某些类是从XML文件生成的,而我拥有.xml文件,但是由于我从没做过这样的事情,所以无论是Java还是C#都没有。

我一直在做一些研究,发现了将.xml文件转换为.cs类的不同方法,但是找不到关于.xml应该具有的语法的文档。

我尝试使用工具xsd.exe,并设法从一个xml文件创建了xsd文件,但是当我尝试生成.cs文件时,却出现以下错误:Error: Can only generate one of classes or datasets.

然后,我进行了一些研究,发现了另一个工具Xsd2Code,因此我尝试将其与相同的.xml文件一起使用以获取.cs文件,但是它提示错误,提示该结构:

C:\Program Files (x86)\Xsd2Code>Xsd2Code.exe Vehicle.xml Vehicle.cs

Xsd2Code Version 3.4.0.32990
Code generation utility from XML schema files.

Error: Declaración XML inesperada. La declaración XML debe ser el primer nodo del documento y no pueden aparecer espacios en blanco delante. Línea 2, posición 3.
        SubType: Unspecified

        Rule:

我将翻译它,因为它是西班牙语:
错误:意外的XML声明。 XML声明必须是第一个节点,并且之前不能包含空格。第2行,位置3。

这是特定文件的第一行:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:hfp="http://www.w3.org/2001/XMLSchema-hasFacetAndProperty" targetNamespace="http://www.w3.org/2001/XMLSchema" blockDefault="#all" elementFormDefault="qualified" version="1.0" xml:lang="EN">
<?xml version="1.0" encoding="UTF-8"?>
<traciClass>
    <name>Vehicle</name>

编辑:正如第一条评论所述,我已将其更正,但现在我收到了

Error: Schema validation failed:
El elemento 'traciClass' no es compatible en este contexto.
        SubType: Unspecified

        Rule:

The element traciClass is not compatible in this context.

原始帖子继续:

这是一个模板,说明了如何在XML for Java中定义不同的对象,但是我想知道这种结构对于C#是否会有所不同。

<?xml version="1.0" encoding="UTF-8"?>
<!-- This file is used to generate a Java class with the same name for a 
    TraCI object. This saves manually writing a lot of boilerplate code. -->
<traciClass>
    <!-- The name of the object. It will be used as the class name. First letter 
        is capital. Must be equal to this document's file name. -->
    <name>ExampleTraciObject</name>

    <!-- The javadoc of the class that will be generated. -->
    <javadoc>
    Put your object description here.
    </javadoc>

    <!-- Lists all the other repositories that are needed by the queries -->
    <repos>
        <repo>Repository1</repo>
        <repo>Repository2</repo>
    </repos>

    <command>it.polito.appeal.traci.protocol.Constants.CMD_GET_VEHICLE_VARIABLE</command>

    <!-- List of all "read" queries, i.e. those that don't change the state 
        of the object and return a value -->
    <readQueries>

        <readQuery>

            <!-- The name of the query. If the name is XXX, the Java class will contain 
                a method named queryXXX() -->
            <name>ReadSomeValueQuery</name>

            <!-- The enum name of the query. It will appear as an enum entry
            in the inner Variable enum, and can be used with the getReadQuery() method -->
            <enum>SOME_VALUE</enum>

            <!-- A numeric value or a constant of type int that tells the variable 
                ID -->
            <const>it.polito.appeal.traci.protocol.Constants.SOME_VARIABLE</const>

            <!-- The Java class name that can make the query. It must be a subclass 
                of ReadObjectVarQuery. If the class is on the package
                it.polito.appeal.traci, the package name can be omitted-->
            <query>ReadObjectVarQuery.IntegerQ</query>

            <!-- The return type of the query. It must be the same type (or a supertype) 
                of the type parameter V specified in the above class. 
                Leave it empty to use the query class as the return type. -->
            <returnType>java.lang.Integer</returnType>

            <!-- If true, it means that this value may change at every simulation 
                step. -->
            <dynamic>true</dynamic>
        </readQuery>

        <!-- add other read queries here -->
    </readQueries>

    <!-- List of all "change state" queries, i.e. those that change the state 
        of the object and don't return a value -->
    <changeStateQueries>

        <!-- The syntax of a changeStateQuery is similar to readQuery, differences 
            are listed below. -->
        <changeStateQuery>
            <name>DoSomething</name>
            <query>DoSomethingQuery</query>
            <!-- Lists the read queries that may be changed by the execution of this 
            query, identified by their name. Calling this query will clear the caches 
            of the queries contained here. -->
            <affects>
                <affect>ReadSomeValueQuery</affect>
            </affects>
        </changeStateQuery>

        <!-- add other change state queries here -->

    </changeStateQueries>

</traciClass>

这是XML语法的问题吗?

参考方案

xsd.exexsd2Code可用于从XML模式(通常以.xsd扩展名,因此是工具名称)生成C#类。它们不会直接从示例XML实例生成类。

根据xsd.exe docs,您可以使用它从XML实例推断模式。然后,您可以由此生成类。

xsd.exe instance.xml
xsd.exe instance.xsd /classes

Visual Studio也可以帮助您:将XML复制到剪贴板,然后单击Edit | Paste Special | Paste XML as Classes

休眠映射<键,设置<值>> - java

我有以下表格:@Entity @Table(name = "events") Event --id --name @Entity @Table(name = "state") State --id --name @Entity @Table(name = "action") Action --id …

无法从ArrayList <String>转换为List <Comparable> - java

当我写下面的代码时,编译器说 无法从ArrayList<String>转换为List<Comparable>private List<Comparable> get(){ return new ArrayList<String>(); } 但是当我用通配符编写返回类型时,代码会编译。private List&l…

合并List <T>和List <Optional <T >> - java

鉴于: List<Integer> integers = new ArrayList<>(Arrays.asList( 10, 12 )); List<Optional<Integer>> optionalIntegers = Arrays.asList( Optional.of(5), Optional.em…

实例化类型<?>的泛型类 - java

我正在为SCJP / OCPJP学习,并且遇到了一个对我来说很奇怪的示例问题。该示例代码实例化了两个通用集合:List<?> list = new ArrayList<?>(); List<? extends Object> list2 = new ArrayList<? extends Object>(); …

List <Dog>是List <Animal>的子类吗?为什么Java泛型不是隐式多态的? - java

我对Java泛型如何处理继承/多态感到困惑。假设以下层次结构-动物(父母)狗-猫(儿童)因此,假设我有一个方法doSomething(List<Animal> animals)。根据继承和多态性的所有规则,我假设List<Dog>是List<Animal>,而List<Cat>是List<Animal&g…