2

I have received a XSD schema MainSchema.XSD and also Common.Xsd schema.

In MainSchema.xsd i have a following line:

<xs:include schemaLocation="Common.xsd"/>

And Common.Xsd hold a definition for various types of data like:

<xs:simpleType name="SSN">
    <xs:annotation>
        <xs:documentation>Social security number is 10 digits</xs:documentation>
        <xs:appinfo>
            <altova:exampleValues>
                <altova:example value="5412983209"/>
                <altova:example value=""/>
            </altova:exampleValues>
        </xs:appinfo>
    </xs:annotation>
    <xs:restriction base="xs:string">
        <xs:whiteSpace value="collapse"/>
        <xs:pattern value="([0-9]{10})?"/>
    </xs:restriction>
</xs:simpleType>

and in MainSchema i have a property called SSNField of type SSN:

<xs:attribute name="CompanySSN" type="SSN">
    <xs:annotation>
        <xs:documentation>SSN number of Company</xs:documentation>
    </xs:annotation>
</xs:attribute>

When i create a c# object class with this command:

xsd.exe -c -l:c# MainSchema.xsd Common.Xsd

it then created a object called:

MainSchema_Common.cs

When i validate an object against this Schema it comes up with an Exception:

{"Type 'http://schemas.domain.com:SSN' is not declared, or is not a simple type."}

Does anyone knows what i´m doing wrong ?

Bear in mynd that i received this XSD schemas from a outside source and i was told that there were no errors in this files.

Sincerly agh

aghaux
  • 729
  • 4
  • 14
  • 38

1 Answers1

1

You need to explain how you are validating. I assume you are creating an instance of the class and then serializing to XML, and the xml is not validating?

You need to be aware that just because your xml is the product of serializing a type derived using xsd.exe does not automatically mean the xml will be compliant to the schema.

You may need to prime the XmlSerializer by injecting an override for the root namespace or indeed other nodes in the document.

For example to inject a namespace at a certain node:

XmlAttributeOverrides overrides = new XmlAttributeOverrides();

var elementAttribute = new XmlElementAttribute(typeof(SSN))
{
    ElementName = "SSN", 
    Namespace = "http://schemas.domain.com:SSN"
};

var newAttribute = new XmlAttributes();
newAttribute.XmlElements.Add(elementAttribute);
overrides.Add(typeof(ParentNodeType), "SSN", newAttribute);

To call the serilaizer:

XmlSerializer serializer = new XmlSerializer(typeof(MyType), overrides); 

Hope this helps

tom redfern
  • 30,562
  • 14
  • 91
  • 126
  • Thanks for the answer, what do you mean by "ParentNodeType" – aghaux Aug 19 '11 at 14:41
  • It looks like you need to inject a namespace for a specific node during serialization. In order to do this you need to tell the serializer the type of the parent node of the node you want the namespace to apply to. I'm not sure why this is, but that's how it works. – tom redfern Aug 19 '11 at 15:44
  • ok great answer thank you, still i wasn´t managed to get this to work. So i just took the code on Common.Xsd, the xsd file that was included and merged it into mainSchema.xsd and re-generated the object and it worked. It´s very frustrated using included schema files and not getting this things to work with the XML validation. – aghaux Aug 19 '11 at 16:32