0

I am currently trying to translate part of a Java library to a C# library for using in into Unity. In this Java library some of the classes are generated from XML files, whose .xml files I have, but since I never did something like this, neither in Java nor in C#.

I have been doing some research and I have found different ways of converting this .xml files to .cs classes, but I cannot find documentation about the syntax that the .xml should have.

I tried to use the tool xsd.exe and I manage to create and xsd file from one xml file but when I tried to generate the .cs file I was propted this error: Error: Can only generate one of classes or datasets.

I then made some research and found another tool, Xsd2Code, so I tried to use it with the same .xml files to obtain a .cs file but it prompted an error complaining about the structure:

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:

I will translate it, since it is in Spanish: Error: Unexpected XML declaration. XML declaration must be the first node and must not contain blank spaces before. Line 2, position 3.

This are the first lines of the specific file:

<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>

EDIT: I corrected it as the first comment says, but now I receive

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.

Original post continues:

This is a template of how the different objects should be defined in XML for Java, but I am wondering whether this structure will vary for 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>

Is it a problem of XML syntax?

4
  • <?xml version="1.0" encoding="UTF-8"?> must be the first line in the file. That's exactly what "XML declaration must be the first node and must not contain blank spaces before." tries to tell you :) Commented May 10, 2016 at 9:39
  • I will edit my post. I changed that and now it says "traciClass is not compatible in this context" Commented May 10, 2016 at 9:46
  • I guss you'll have to transform it into a format that your C# Code generator expects. For example "javadoc" would be an obvious case of a tag that is useless for c# but probably just needs renaming. Commented May 10, 2016 at 10:31
  • Yes, of course, javadoc needs to be eliminated. The main question would be how to make the proper syntax, I only put the template so my post could be more complete Commented May 10, 2016 at 10:39

1 Answer 1

1

xsd.exe and xsd2Code can be used for generating C# classes from an XML schema (usually with the extension .xsd, hence the tool names). They will not generate classes directly from a sample XML instance.

Per the xsd.exe docs, you can use it to infer a schema from an XML instance. You could then generate classes from this.

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

Visual Studio can also help you with this: copy your XML to the clipboard and click Edit | Paste Special | Paste XML as Classes.

Sign up to request clarification or add additional context in comments.

7 Comments

Two comments regarding this: First, for some reason when I do the Edit -> Paste Special -> Paste XML Classes it doesn't work. Any kind of message, warning, or anything is being prompted, but nothing is pasted. Second, I had also read that the xsd.exe could be used to generate the .xsd file from the .xml file, right?
It works for me. You're right re inferring an xsd from the xml - I've updated my answer. You need to do this in two steps, it seems.
Mmm... I keep receiving this error when doing xsd.exe Vehicle.xml Error: Error al procesar 'Vehicle.xml'. - El elemento 'traciClass' no es compatible en este contexto.
Basicly it says traciClass is not compatible
Not sure what to suggest. I can Paste XML as Classes and use xsd.exe using the XML I've just copy & pasted from your question.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.