Question

I have following xsd:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema
        targetNamespace="http://www.something.com/GetWrapRequest"
        elementFormDefault="qualified" attributeFormDefault="qualified" version="1.0"
        xmlns:xsd="http://www.w3.org/2001/XMLSchema"
        xmlns:gwreq="http://www.something.com/GetWrapRequest">

    <xsd:element name="message" type="gwreq:Message">
        <xsd:annotation>
            <xsd:documentation>Complete message</xsd:documentation>
        </xsd:annotation>
    </xsd:element>
    <xsd:complexType name="Message">
        <!-- something here -->
    </xsd:complexType>
</xsd:schema>

And for generating C# class I am using modified code from http://hosca.com/blog/post/2008/12/26/Generating-C-classes-from-FpML-Schema.aspx I cannot use usual xsd.exe because I need to create C# namespaces from XML namespaces and xsd.exe is placing all classes to one C# namespace. So I found this piece of code and I exteded it to create correct namespaces. But all the parts related to converting xsd to CodeDom is still the same.

My problem now is that the xsd.exe is generating this:

[System.Xml.Serialization.XmlRootAttribute("message", Namespace="http://www.something.com/GetWrapRequest", IsNullable=true)]
public partial class Message {}

and my code is generating this:

[System.Xml.Serialization.XmlRootAttribute(Namespace="http://www.something.com/GetWrapRequest", IsNullable=true)]
public partial class Message {}

As you can see the "message" with lower "m" is missing in the attribute. And because the xml I need to deserialize is also with the tag "message" with lower "m" deserialization fails.

How can I solve this? I looked at options of XmlSchemaImporter and XmlCodeExporter but nothing can do the trick. Or can I somehow set up XmlSerializer to disable case sensitivity?

Was it helpful?

Solution

So after sneaking in the Xsd2Code source code I found interesting thing. I am using these two loops to create xml mapping

foreach (XmlSchemaType schemaType in rootSchema.SchemaTypes.Values)
    xmlTypeMappings.Add(xmlSchemaImporter.ImportSchemaType(schemaType.QualifiedName));
foreach (XmlSchemaElement schemaElement in rootSchema.Elements.Values)
    xmlTypeMappings.Add(xmlSchemaImporter.ImportTypeMapping(schemaElement.QualifiedName));

But in the Xsd2Code they are processing elements first and schema types after it. So I just change the order of these to loops like this:

foreach (XmlSchemaElement schemaElement in rootSchema.Elements.Values)
    xmlTypeMappings.Add(xmlSchemaImporter.ImportTypeMapping(schemaElement.QualifiedName))
foreach (XmlSchemaType schemaType in rootSchema.SchemaTypes.Values)
    xmlTypeMappings.Add(xmlSchemaImporter.ImportSchemaType(schemaType.QualifiedName));

And proper XmlRootAttribute with the element name "message" is generated.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top