Generating C# classes from XML schema that use JAXB (Java Architecture for XML Binding)
I have a number of XML schema that use JAXB (Java Architecture for XML Binding) markup, similar to the following:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:jxb="http://java.sun.com/xml/ns/jaxb" jxb:version="1.0">
<xs:element name="H">
<xs:annotation>
<xs:appinfo>
<jxb:class name="Header" />
</xs:appinfo>
</xs:annotation>
<xs:complexType>
<xs:sequence>
<xs:element ref="X" minOccurs="1" maxOccurs="1">
<xs:annotation>
<xs:appinfo>
<jxb:property name="SessionControl"/>
</xs:appinfo>
</xs:annotation>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="X" type="xs:string">
<xs:annotation>
<xs:appinfo>
<jxb:class name="SessionControl"/>
</xs:appinfo>
</xs:annotation>
</xs:element>
</xs:schema>
From these I need to generate C# classes.
Running xsd.exe on the above schema generates the following (some attributes removed for clarity):
[XmlRoot(Namespace = "", IsNullable = false)]
public partial class H
{
private string xField;
public string X
{
get { return this.xField; }
set { this.xField = value; }
}
}
Clearly this is not ideal - I need to take account of the 开发者_JAVA技巧jxb:class
and jxb:property
markup in the schema, giving me a class that uses the more readable jxb
names, but that serializes to the single-character elements/attributes specified in the schema.
e.g.
[XmlRoot(Namespace = "", IsNullable = false, ElementName = "H")]
public partial class Header
{
private string xField;
[XmlElement(ElementName = "X")]
public string SessionControl
{
get { return this.xField; }
set { this.xField = value; }
}
}
Is there any reasonable route to achieving this?
I know that on the java side, it is possible to tell the jaxb processor about the extra binding information in three different ways:
- In the xsd itself like you presented in your question.
- In a separate xml file that the tool uses.
- In annotations on the java classes themselves.
All these ways are semantically equivalent - so strictly speaking, the <jxb> tags are not part of the xsd contract. One could use options 2. or 3. above to construct slightly different java code but with the same xsd and wsdl contract.
I expect a similar mechanism to exist within the toolset on .NET side. I suggest that you lookup the reference for xsd.exe and related information, to see what support is there for the customization of the generated code (sorry I can't assist more, I am not familiar with this tool).
The approach we used for this in the end was to just do the work manually - from the xsd.exe generated C# source, using the xsd files as a reference.
精彩评论