Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid regenerating the same types over and over #497

Open
digitaldias opened this issue Mar 13, 2024 · 3 comments
Open

Avoid regenerating the same types over and over #497

digitaldias opened this issue Mar 13, 2024 · 3 comments

Comments

@digitaldias
Copy link

digitaldias commented Mar 13, 2024

I have a full set of XSD's, some which declare common types that are used in others.
I don't know how I can run this tool (CLI or Nuget) to only generate the common types once. Our xsd's include:

  • Common types
  • Orders
  • Invoices
  • OrderConfirmations

Etc. it's a whole tree of different standards for these doc types.

An option to "do not include external xsd's" would help me, as I can then just add the correct "using" statements post creation. Is this possible to achieve? Concrete example:

  • I extract the common types xsd into namespace MyBusiness.Standards.Standard1.v1_1.Common
  • When I extract "Orders" xsd, it goes into namespace MyBusiness.Standards.Standard1.v1_1.Orders but with an instruction to include a using statement for the first namespace.

Today, it reads and includes referenced xsd files and generates all the common types for every type that I have. A command line example could be:

> xsd2cs -n "Orders" --add-using "Common" --do-not-include -o Test .\Orders.xsd

Doable?

@mganss
Copy link
Owner

mganss commented Mar 13, 2024

I'm not sure I understand the issue. For each distinct XML Schema type there should be only one corresponding C# class that is being generated. Where does the duplication occur in your use case?

@digitaldias
Copy link
Author

I may be attempting to convert too much at once:

var xsdFiles = XsdFileFinder.FindXsdFilesWithNamespaces(rootFolder);

Console.WriteLine(new string('-', 120));

var namespaceMappings = new Dictionary<NamespaceKey, string>();

foreach (var xsdFile in xsdFiles)
{
    var key = new NamespaceKey(xsdFile.XsdNamespace);
    if (!namespaceMappings.ContainsKey(key))
    {
        namespaceMappings.Add(key, xsdFile.CSharpNamespace);
    }
}

var NsProvider = namespaceMappings
    .ToNamespaceProvider(new GeneratorConfiguration { NamespacePrefix = string.Empty }
    .NamespaceProvider.GenerateNamespace);

When I attempt to give the entire list to the generator, it fails profusely. with variants of The element has already been declarederrors.

If I run it in a foreach loop, all is good, but common types are repeated across the board.

@mganss
Copy link
Owner

mganss commented Mar 19, 2024

Try passing only the XSD file to the generator that contains the root element resp. the elements that you are interested in de/serializing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants