mganss / XmlSchemaClassGenerator

Generate C# classes from XML Schema files
Apache License 2.0
589 stars 179 forks source link

Deserialization produces empty result #492

Closed sniarn closed 6 months ago

sniarn commented 6 months ago

Hi,

Firstly, let it be known that I'm not knowledgeable when it comes to the more technical sides of XML so there's a good chance this is just user error.

I'm having an issue where deserializing produces an empty object (properties are null and collections are empty). I use the following procedure to generate the C# classes:

  1. Open the directory from the ZIP in Visual Studio: example.zip
  2. Generate the XSD files by opening the eu-regional.xml file and selecting the command XML > Create Schema. It is my understanding that Visual Studio will also use the referenced DTD files to perform this operation. Three XSD files are produced by this: eu-regional.xsd, eu-regional1.xsd, and eu-regional2.xsd.
  3. Run xscgen like below:
xscgen -n "http://tempuri.org/eu-regional=EuRegional" -n "uri:xlink=XLink" -n "uri:eu=EuRegional" --disableComments --nullable --verbose --output=output .\eu-regional.xsd .\eu-regional1.xsd .\eu-regional2.xsd

I'm then parsing the XML file using the code below, but an "empty" object is returned. There is no error.

var xmlReaderSettings = new XmlReaderSettings
{
    DtdProcessing = DtdProcessing.Ignore
};
var xmlSerializer = new XmlSerializer(typeof(EuBackbone));
var xmlReader = XmlReader.Create(input, xmlReaderSettings);
EuBackbone parsed = (EuBackbone)xmlSerializer.Deserialize(xmlReader);

Anyone have an idea what the problem is?

Thanks.

sniarn commented 6 months ago

This is using Version 2.1.1107.0.

mganss commented 6 months ago

I have a feeling that this is a namespace issue. When opening the XML file in VS, I get the following errors:

The element 'eu-backbone' in namespace 'http://europa.eu.int' has invalid child element 'eu-envelope'. List of possible elements expected: 'eu-envelope' in namespace 'uri:'.

The generated XSD contains xmlns="uri:" which looks weird.

mganss commented 6 months ago

Can you try a different DTD to XSD conversion tool like the W3C one?

sniarn commented 6 months ago

Thanks for replying.

We ended up manually adjusting the namespaces in the generated output files and got it working that way, so it is indeed a namespace issue. We don't see a need to re-generate the schema all that often, if at all, so we've settled on this as a solution that works for us. I might try the Perl script if I get some free time this week.