XML 生成 C# 代码的语法
XML Syntax for generating C# code
我目前正在尝试将 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.
原文post继续:
这是 XML 中应如何为 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.exe
和 xsd2Code
可用于从 XML schema 生成 C# 类(通常带有扩展名.xsd,因此是工具名称)。它们不会直接从样本 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 库的一部分转换为 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.
原文post继续:
这是 XML 中应如何为 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.exe
和 xsd2Code
可用于从 XML schema 生成 C# 类(通常带有扩展名.xsd,因此是工具名称)。它们不会直接从样本 XML 实例 .
根据 xsd.exe
docs,您可以使用它从 XML 实例推断模式。然后您可以从中生成 类。
xsd.exe instance.xml
xsd.exe instance.xsd /classes
Visual Studio 也可以帮助您:将您的 XML 复制到剪贴板并单击 Edit | Paste Special | Paste XML as Classes
。