C# code generation with xsd.exe "how to ?"

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Marek

    #1

    C# code generation with xsd.exe "how to ?"

    Actually, the very first question I should ask is "does it make sense at
    all?".
    For 2 days I've been trying to generate C# classes using XML schema and
    xsd.exe tool. So far the results are not exciting but I assume I am
    seriously short of knowledge, so here I am - asking for help. My current
    situation is that I can define schemas that will generate very simple
    classes but I have two major problems:

    (1) I do not know how to write a schema, so that my classes can get
    properties with skeletons for getter and setter, as opposed to public fields
    ? Right now, for every generated class, all the attributes defined in xsd,
    get converted into public fields, not properties.

    (2) How to write a schema to get class structure that exposes inheritance
    and incorporates interfaces ?
    I try to use "complexContent " with "extension" and "base" attributes but
    when compiling the schema with xsd.exe I am getting different kind of errors
    (i.e. "cannot generate classes because no top-level elements with complex
    type were found" or "cannot generate classes because no top-level elements
    with complex type were found" ). I even tried to compile an example from
    MSDN (Person->ExtendedPerson ) but no success at all...

    My friend is strongly advocating gsl-gen (a third-party code generator) but
    I would really like to be able getting that work done using .net
    infrastructure, without any additional pieces.
    I would appreciate any useful hints, especially some exmaples for my two
    problems that I mentioned above. Thank you very much,

    Marek


  • Dino Chiesa [Microsoft]

    #2
    Re: C# code generation with xsd.exe "how to ?"


    "Marek" <nospam@nospam. com> wrote in message
    news:Km6Fc.8954 3$Ax1.1118454@n ews20.bellgloba l.com...[color=blue]
    > Actually, the very first question I should ask is "does it make sense at
    > all?".
    > For 2 days I've been trying to generate C# classes using XML schema and
    > xsd.exe tool. So far the results are not exciting but I assume I am
    > seriously short of knowledge, so here I am - asking for help. My current
    > situation is that I can define schemas that will generate very simple
    > classes but I have two major problems:
    >
    > (1) I do not know how to write a schema, so that my classes can get
    > properties with skeletons for getter and setter, as opposed to public[/color]
    fields[color=blue]
    > ? Right now, for every generated class, all the attributes defined in xsd,
    > get converted into public fields, not properties.
    >[/color]

    xsd.exe generates public fields. If you like you can take the generated
    code and modify it. For example,



    You can also try XsdObjectGen - see


    [color=blue]
    > (2) How to write a schema to get class structure that exposes inheritance
    > and incorporates interfaces ?
    > I try to use "complexContent " with "extension" and "base" attributes but
    > when compiling the schema with xsd.exe I am getting different kind of[/color]
    errors[color=blue]
    > (i.e. "cannot generate classes because no top-level elements with complex
    > type were found" or "cannot generate classes because no top-level[/color]
    elements[color=blue]
    > with complex type were found" ). I even tried to compile an example from
    > MSDN (Person->ExtendedPerson ) but no success at all...
    >[/color]

    Why don't you try going the other direction? Build your object class, then
    run xsd.exe on the resulting assembly (dll or exe or ..) and then examine
    the inferred schema.

    [color=blue]
    > My friend is strongly advocating gsl-gen (a third-party code generator)[/color]
    but[color=blue]
    > I would really like to be able getting that work done using .net
    > infrastructure, without any additional pieces.
    > I would appreciate any useful hints, especially some exmaples for my two
    > problems that I mentioned above. Thank you very much,
    >
    > Marek
    >
    >[/color]


    Comment

    • Daniel Cazzulino [MVP XML]

      #3
      Re: C# code generation with xsd.exe &quot;how to ?&quot;

      1) You don't need to use third party software to get public properties or
      strong typed collections instead of arrays. Check the article titled Code
      Generation in the .NET Framework Using XML Schema on MSDN
      (http://msdn.microsoft.com/xml/defaul...s/dnxmlnet/htm
      l/xsdcodegen.asp) .
      Another quick trick you can use is this:


      2) In order to get classes that inherit from others, you have to create
      complex types that act as the base type for others. For example, you have to
      define a Person complex type, and make the ExtendedPerson complex type
      inherit from it. You'll get two classes, the later inheriting the former.

      --
      Daniel Cazzulino [MVP XML]
      Clarius Consulting SA




      "Marek" <nospam@nospam. com> wrote in message
      news:Km6Fc.8954 3$Ax1.1118454@n ews20.bellgloba l.com...[color=blue]
      > Actually, the very first question I should ask is "does it make sense at
      > all?".
      > For 2 days I've been trying to generate C# classes using XML schema and
      > xsd.exe tool. So far the results are not exciting but I assume I am
      > seriously short of knowledge, so here I am - asking for help. My current
      > situation is that I can define schemas that will generate very simple
      > classes but I have two major problems:
      >
      > (1) I do not know how to write a schema, so that my classes can get
      > properties with skeletons for getter and setter, as opposed to public[/color]
      fields[color=blue]
      > ? Right now, for every generated class, all the attributes defined in xsd,
      > get converted into public fields, not properties.
      >
      > (2) How to write a schema to get class structure that exposes inheritance
      > and incorporates interfaces ?
      > I try to use "complexContent " with "extension" and "base" attributes but
      > when compiling the schema with xsd.exe I am getting different kind of[/color]
      errors[color=blue]
      > (i.e. "cannot generate classes because no top-level elements with complex
      > type were found" or "cannot generate classes because no top-level[/color]
      elements[color=blue]
      > with complex type were found" ). I even tried to compile an example from
      > MSDN (Person->ExtendedPerson ) but no success at all...
      >
      > My friend is strongly advocating gsl-gen (a third-party code generator)[/color]
      but[color=blue]
      > I would really like to be able getting that work done using .net
      > infrastructure, without any additional pieces.
      > I would appreciate any useful hints, especially some exmaples for my two
      > problems that I mentioned above. Thank you very much,
      >
      > Marek
      >
      >[/color]


      ---
      Outgoing mail is certified Virus Free.
      Checked by AVG anti-virus system (http://www.grisoft.com).
      Version: 6.0.708 / Virus Database: 464 - Release Date: 19/06/2004


      Comment

      Working...