Page 1 of 1

Find optimum nested level in XML generation

Posted: Sun Sep 14, 2025 1:20 pm
by Niranjan
Hello All,

I have a set of complex xsd files where there are imports/includes across all the files. I am trying to generate a sample xml file using these xsd files from "Generate Sample XML Files" option. The aim is to generate the sample xml file, paste the xml in Visual Studio 2022 as class, generate a dll (class library) and to deserialize another xml (with actual data) which was generated using same xsds. The generated .cs file also is used in other classes in the project.

I am at wits end as I am not able to get the right configuration for "Generate Sample XML Files" as for every combination I get different issues. Sometimes instead of collections I get objects (setting "preferred number of options to 2 generated List in .cs file but the classes repeat), sometimes elements go missing, file size too big etc.

The user manual says discardOptionalElementsAfterNestedLevel can generate large files which is correct. How do I check the optimum setting for this variable?

Image shows file sizes generated for different discardOptionalElementsAfterNestedLevel settings

Capture.JPG
Thanks in advance.
Niranjan

Re: Find optimum nested level in XML generation

Posted: Fri Sep 19, 2025 12:13 pm
by teo
Hello Niranjan,

The behavior you are seeing is expected, since the "Generate Sample XML Files" action is meant primarily for creating an example instance for testing/documentation, not for producing a canonical instance that can be used directly for code generation.

The setting in question - "Discard optional elements after nested level" - controls how many levels of nested optional elements are generated before they are discarded.
If you set a low value, optional elements deeper than that level are skipped, meaning the file gets smaller, but content may appear incomplete.
If you set a high value, more optional elements are generated, and file size grows exponentially.

There isn’t a universal "optimum" value – it depends on the complexity of your schemas and how much of the optional structure you actually need in the sample. Often values between 2–4 are enough to demonstrate structure without exploding file size.

Best regards,
Teo