XSD classes in C # with name formatting control

I'm working with an XSD file that uses UPPERCASE for element names and an inconsistent wrapper for the attribute name. Unfortunately, this cannot be fixed because XML files that conform to this schema are read and written by programs that are hard-coded to expect these conventions.

I am using Visual Studio 2013 tool xsd.exe

to transform XSD file into model classes, however XSD copies element and types-names verbatim and applies only attributes [XmlElement]

or [XmlAttribute]

when the name isn't legal in C # (for example when the name contains a dash character).

Given XML file:

<someIndustrialData>
    <FOOBAR name="foo" baz="BAR">
        <CHILDELEMENTS>
            <FOOBARCHILDELEMENT QUX="FOO">


            </FOOBARCHILDELEMENT>
            <FOOBARCHILDELEMENT qux="foo">

            </FOOBARCHILDELEMENT>
        </CHILDELEMENTS>
    </FOOBAR>
</someIndustrialData>

      

Hence, I have the xsd.exe

generated output as follows:

public partial class FOOBAR {
    public FOOBARCHILDELEMENT[] CHILDELEMENTS { get; set; }
}
public partial class FOOBARCHILDELEMENT {
    public String QUX { get; set; }
    public String qux { get; set; }
}

      

I do not like this.

I would prefer it to xsd.exe

generate this output:

[XmlElement("FOOBAR")]
public partial class FooBar {

    [XmlElement("CHILDELEMENTS")]
    public FooBarChild[] Children { get; set; }
}

[XmlElement("FOOBARCHILDELEMENT ")]
public partial class FooBarChild {

    [XmlAttribute("QUX")
    private String QUX { get; set; }

    [XmlAttribute("qux")
    private String qux { get; set; }

    public String Qux { get { return this.QUX ?? this.qux; } }
}

      

i.e:.

  • Names of generated POCO types and their members to comply with .NET conventions ( PascalCase

    instance instead of UPPERCASE

    )

  • The generated POCO types must have attributes XmlElement

    and XmlAttribute

    therefore the classes XmlSerializer

    can map them to XML elements without using the POCO type name.

Is it possible?

+3


source to share


2 answers


This is probably not the answer you are looking for, but I would recommend living with an unsightly capitalization convention simply because it best reflects the actual interface to your service. Why allow slavish adherence to .NET conventions to create additional non-functional work for you and additional support for you later?

If your purist senses of your team are offended by these admittedly disgusting names, consider that a more productive use of design time might be to create a non-frontend model suitable for your domain. Coding directly against the interface model is not ideal for significant projects. If your project is fast and messy, or otherwise small, bad names won't spread far. If your project is significant, let the bad names accurately reflect the true interface names and create separate, non-interface domain-based classes that provide a better model for your code (and second, can follow your preferred naming conventions).



If you have or have additional interfaces to support (other XML schemas, JSON, etc.), if the domain code goes against domain objects rather than interface objects, it will be more beneficial in the long run than the .NET commitment. naming conventions in interface classes.

+2


source


This is an old post, but having the same problem, here is my solution. You can control the class name generated by xsd.exe with named complexTypes like:

         // some code   
     <xs:element ref="person"/>
         // some code

  <xs:element name="person" type="PersonData"/>
  <xs:complexType name="PersonData">
          // some code
  </xs:complexType>

      

Here , a PersonData class has been created for the person XML element . The generated output will look like:



public partial class PersonData {
   // generated class code
}

      

The result is that xsd.exe will generate the PersonData class for the person of the item.

0


source







All Articles