XmlSchemaClassGenerator icon indicating copy to clipboard operation
XmlSchemaClassGenerator copied to clipboard

How to generate common types once and avoid XmlSerializer generator type collisions for MeterInfoType?

Open snikeguo opened this issue 1 month ago • 2 comments

schemas.zip

Short background We have two shared XSDs at the repo root (V2G_CI_AppProtocol.xsd and xmldsig-core-schema.xsd) that should be generated once into HomePlug.V2G.Common. Three standards folders (din_spec, iso15118_2, iso15118_20) each reference those two shared XSDs and should generate their own C# into profile-specific namespaces (e.g. HomePlug.V2G.DIN70121, HomePlug.V2G.ISO15118_2, HomePlug.V2G.ISO15118_20). When generating code for each standard, the shared XSD types must be resolvable for imports/includes but must not be regenerated (no duplicate types).

Problem 1 — generation / configuration question When generating the three standard outputs, what is the recommended project JSON / generator options to:

resolve imports/includes to the root XSDs so generation succeeds, and avoid emitting duplicate types from V2G_CI_AppProtocol.xsd and xmldsig-core-schema.xsd into each standard assembly? Concretely: does the generator support an “excludeSchemas” (or equivalent) option, or should we rely on searchPaths and explicitly omit the root XSDs from each standard’s schema list? A minimal example JSON for:

HomePlug.V2G.Common (generating the two root XSDs), and one standard project (e.g. din_spec) that resolves imports but does not re-emit the common types would be very helpful. Problem 2 — XmlSerializer generator error (runtime / build) After generating the C# sources, running Microsoft.XmlSerializer.Generator (sgen) fails with a type collision for MeterInfoType because multiple generated assemblies include types with the same XML element name. Example build output:

[.NET Xml Serialization Generation Utility, Version 10.0.0] The top XML element 'MeterInfoType' from namespace '' references distinct types HomePlug.V2G.ISO15118_2.MeterInfoType and HomePlug.V2G.ISO15118_20.MeterInfoType. Use XML attributes to specify another XML name or namespace for the element or types. C:\Users\Administrator.nuget\packages\microsoft.xmlserializer.generator\10.0.0\build\Microsoft.XmlSerializer.Generator.targets(51,5): warning MSB3073: 命令“dotnet Microsoft.XmlSerializer.Generator "obj\X64\Debug\net10.0\HomePlug.dll" --force --quiet obj\X64\Debug\net10.0\sgen.rsp”已退出,代码为 1。 C:\Users\Administrator.nuget\packages\microsoft.xmlserializer.generator\10.0.0\build\Microsoft.XmlSerializer.Generator.targets(52,5): warning : SGEN: Failed to generate the serializer for HomePlug.dll. Please follow the instructions at https://go.microsoft.com/fwlink/?linkid=858594 and try again.

snikeguo avatar Nov 27 '25 09:11 snikeguo

Generated.zip

I generate a common assembly and three per-standard assemblies with xscgen. Commands I run:

.\xscgen.exe ^
-n "|V2G_CI_AppProtocol.xsd=HomePlug.V2G.Common" ^
-n "|xmldsig-core-schema.xsd=HomePlug.V2G.Common" ^
schemas\V2G_CI_AppProtocol.xsd ^
schemas\xmldsig-core-schema.xsd ^
-o Generated/Common

.\xscgen.exe ^
-n "|V2G_CI_MsgHeader.xsd=HomePlug.V2G.DIN70121" ^
-n "|V2G_CI_MsgDef.xsd=HomePlug.V2G.DIN70121"  ^
-n "|V2G_CI_MsgDataTypes.xsd=HomePlug.V2G.DIN70121" ^
-n "|V2G_CI_MsgBody.xsd=HomePlug.V2G.DIN70121" ^
-n "|xmldsig-core-schema.xsd=HomePlug.V2G.Common"  ^
schemas\din_spec\V2G_CI_MsgHeader.xsd  ^
schemas\din_spec\V2G_CI_MsgDef.xsd  ^
schemas\din_spec\V2G_CI_MsgDataTypes.xsd ^
schemas\din_spec\V2G_CI_MsgBody.xsd ^
schemas\xmldsig-core-schema.xsd  ^
-o Generated/DIN70121

.\xscgen.exe ^
-n "|V2G_CI_MsgHeader.xsd=HomePlug.V2G.ISO15118_2" ^
-n "|V2G_CI_MsgDef.xsd=HomePlug.V2G.ISO15118_2"  ^
-n "|V2G_CI_MsgDataTypes.xsd=HomePlug.V2G.ISO15118_2" ^
-n "|V2G_CI_MsgBody.xsd=HomePlug.V2G.ISO15118_2" ^
-n "|xmldsig-core-schema.xsd=HomePlug.V2G.Common"  ^
schemas\iso15118_2\V2G_CI_MsgHeader.xsd  ^
schemas\iso15118_2\V2G_CI_MsgDef.xsd  ^
schemas\iso15118_2\V2G_CI_MsgDataTypes.xsd  ^
schemas\iso15118_2\V2G_CI_MsgBody.xsd  ^
schemas\xmldsig-core-schema.xsd  ^
-o Generated/ISO15118_2

.\xscgen.exe ^
-n "|V2G_CI_AC.xsd=HomePlug.V2G.ISO15118_20" ^
-n "|V2G_CI_ACDP.xsd=HomePlug.V2G.ISO15118_20"  ^
-n "|V2G_CI_CommonMessages.xsd=HomePlug.V2G.ISO15118_20" ^
-n "|V2G_CI_CommonTypes.xsd=HomePlug.V2G.ISO15118_20" ^
-n "|V2G_CI_DC.xsd=HomePlug.V2G.ISO15118_20" ^
-n "|V2G_CI_WPT.xsd=HomePlug.V2G.ISO15118_20" ^
-n "|xmldsig-core-schema.xsd=HomePlug.V2G.Common"  ^
schemas\iso15118_20\V2G_CI_AC.xsd  ^
schemas\iso15118_20\V2G_CI_ACDP.xsd  ^
schemas\iso15118_20\V2G_CI_CommonMessages.xsd  ^
schemas\iso15118_20\V2G_CI_CommonTypes.xsd  ^
schemas\iso15118_20\V2G_CI_DC.xsd  ^
schemas\iso15118_20\V2G_CI_WPT.xsd  ^
schemas\xmldsig-core-schema.xsd  ^
-o Generated/ISO15118_20

In the project I remove duplicate Common.cs files:

<Compile Remove="V2G\DIN70121\HomePlug.V2G.Common.cs" />
<Compile Remove="V2G\ISO15118_20\HomePlug.V2G.Common.cs" />
<Compile Remove="V2G\ISO15118_2\HomePlug.V2G.Common.cs" />

Problem: Microsoft.XmlSerializer.Generator fails during build with errors like: "The top XML element 'MessageHeaderType' from namespace '' references distinct types HomePlug.V2G.ISO15118_2.MessageHeaderType and HomePlug.V2G.ISO15118_20.MessageHeaderType."

Image

snikeguo avatar Nov 27 '25 10:11 snikeguo

ISO15118-20

 [System.CodeDom.Compiler.GeneratedCodeAttribute("XmlSchemaClassGenerator", "1.0.0.0")]
 [System.SerializableAttribute()]
 [System.Xml.Serialization.XmlTypeAttribute("MessageHeaderType", Namespace="urn:iso:std:iso:15118:-20:CommonTypes")]
 [System.Diagnostics.DebuggerStepThroughAttribute()]
 [System.ComponentModel.DesignerCategoryAttribute("code")]
 public partial class MessageHeaderType

ISO15118-2

 [System.CodeDom.Compiler.GeneratedCodeAttribute("XmlSchemaClassGenerator", "1.0.0.0")]
 [System.SerializableAttribute()]
 [System.Xml.Serialization.XmlTypeAttribute("MessageHeaderType", Namespace="urn:iso:15118:2:2013:MsgHeader")]
 [System.Diagnostics.DebuggerStepThroughAttribute()]
 [System.ComponentModel.DesignerCategoryAttribute("code")]
 public partial class MessageHeaderType

snikeguo avatar Nov 27 '25 10:11 snikeguo