0

When using XSD.exe to generate classes from XSD files, a certain problem arises for the thing I am trying to do.

I have several (~10) XSD files that are dependent on each other, top-down, with some cross relations as well. These files can be translated into C# files using the XSD.exe tool. Because these are dependent on each other, they should all be generated within one command.

xsd.exe /c xsdfile1.xsd xsdfile2 xsdfile3 xsdfile4 /n:My.csharp.namespace

This command works fine, except for classes that have similar names.
Say there is a class named "User" in xsdfile1.xsd, and there's a class with a similar name "User" in 'xsdfile3.xsd', after the generation, these classes will be named 'User' and 'User1', because there would be a conflict when they would have the same name.

My question is as follows: Is it possible to, instead of adding a number at the end of the class name, separate these classes into multiple namespaces (better multiple files with multiple namespaces), so they can have the same name? Using the xsd.exe tool if possible.

I am unfortunately unable to rename duplicate class names across multiple files since these XSD files are already widely used within the company I am working for.

Example:

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.8.3928.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(TypeName="MyObject", Namespace="http://www.example.com/schemas/Extension")]
[System.Xml.Serialization.XmlRootAttribute("myobject", Namespace="http://www.example.com/schemas/Extension", IsNullable=false)]
public partial class MyObject1 : MyObject {
    /// several properties here
}

MyObject1 shouldn't have an added 1 at the end. This could be fixed if the generator did the following:

public partial class MyObject : My.other.namespace.MyObject {
}
Jurjen
  • 1
  • 3
  • hi, do you have an example so that the problem can be reproduced? Xsd2code has far more options when it comes to these kind of problems. Xsd.exe is very limited. – martijn Oct 13 '20 at 11:51
  • @martijn Unfortunately I can not provide you with a sample of any kind, but I added an example to clarify the problem. – Jurjen Oct 13 '20 at 12:18
  • @martijn As of Xsd2code, I have been trying to use that as well. Although it has more options, I have not found anything that could solve this problem in Xsd2code as well. – Jurjen Oct 13 '20 at 12:20
  • Jurjen, with xsd2code you can create an cs file per xsd. and each cs file can have it's own C# namespace. you can refer to that namespace when generating cs files that requires content from the earlier generated cs files. I cannot reproduce your myobject myobject1 problem. it would be very helpful to have 2 or 3 small xsd's and the list of parameters that you use to execute xsd.exe – martijn Oct 14 '20 at 05:15
  • @martijn How can I refer to that previously generated namespace? That might solve the problem. – Jurjen Oct 14 '20 at 08:00
  • use the /cu option and specify the C# namespace. if you need more than one you separate them with a , make sure there is no space between the namespaces. – martijn Oct 14 '20 at 08:07
  • @martijn I can see what that does, just add the extra 'using' fields with namespaces at the top. That alone doesn't quite solve the problem yet, but I thank we're getting somewhere here. – Jurjen Oct 14 '20 at 09:06
  • @martijn (Sorry for multiple comments, I expect that 'enter' makes a new line, not submit the comment). Is there a possibility to, instead of defining these namespaces on top with 'using', insert them in front of the imported types? Just like the example above: 'My.other.namespace.MyObject' – Jurjen Oct 14 '20 at 09:08
  • I don't think so as the tool generates the code. But still I'm not able to reproduce your problem. Regardless of what I do, single or multiple files, namespace or no namespaces, types defintions or elements I never seem to get an objectname that is made unique with a decimal behind it. – martijn Oct 15 '20 at 05:07
  • That is strange. What happens when you extend from an object of the same name from another file/namespace? – Jurjen Oct 15 '20 at 09:11
  • In the end I was able to replicate the problem. you have a base complex type with an extended complex type that is referring to the base. That is quite exceptional, i never encountered an schema like this. But unfortunately it seems that both xsd.exe and xsd2code.exe are not able to use the other notation. – martijn Oct 15 '20 at 12:13

0 Answers0