2

I need to use a complex type defined in a different assembly in my xsd schema. Both of my .xsd schemas are defined as embedded resources and I've tried to link the sheet I have to import in the assembly, who needs it with no results.

Basically, when I need to validate one of my xml pages, I call this function, but it isn't able to add in cascade the xml schema sets of the types inside Operations.

public static XmlSchema GetDocumentSchema(this Document doc)
{
    var actualType = doc.GetType();
    var stream = actualType.Assembly.GetManifestResourceStream(actualType.FullName);

    if (stream == null)
    {
        throw new FileNotFoundException("Unable to load the embedded file [" + actualType.FullName + "]");
    }

    var documentSchema = XmlSchema.Read(stream, null);

    foreach (XmlSchemaExternal xmlInclude in documentSchema.Includes)
    {
        var includeStream = xmlInclude.SchemaLocation != "Operations.xsd" 
            ? actualType.Assembly.GetManifestResourceStream(xmlInclude.Id) 
            : typeof (Operations).Assembly.GetManifestResourceStream(xmlInclude.Id);

        if (includeStream == null)
        {
            throw new FileNotFoundException("Unable to load the embedded include file [" + xmlInclude.Id + "]");
        }

        xmlInclude.Schema = XmlSchema.Read(includeStream, null); 
    }

    return documentSchema;
}

This is the main schema:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="ExampleSheet"
       attributeFormDefault="unqualified"
       elementFormDefault="qualified"
       xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:include id="Operations" schemaLocation="Operations.xsd"/>
  <xs:element name="ExampleSheet">
    <xs:complexType>
      <xs:sequence>
      <xs:element name="Operations" type="Operations"/>
    </xs:sequence>
    <xs:attribute name="version" type="xs:string" use="required"/>
  </xs:complexType>
  </xs:element>
</xs:schema>    

And this is the schema of Operations:

<xs:schema id="Operations"    
  elementFormDefault="qualified"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
<xs:element name="Operations" type="Operations"/>
  <xs:complexType name="Operations">
    <xs:choice minOccurs="1" maxOccurs="unbounded">
      <xs:element name="Insert" type="Insert"/>
      <xs:element name="InsertUpdate" type="InsertUpdate"/>
      <xs:element name="Update" type="Update"/>
      <xs:element name="Delete" type="Delete"/>
    </xs:choice>
    <xs:attribute name="version" type="xs:string" use="required"/>
    <xs:attribute name="store" type="xs:string" use="required"/>
    <xs:attribute name="chain" type="xs:string" use="optional"/>
  </xs:complexType>
</xs:schema>

For example, if I have an ExampleSheet with an Insert, it isn't able to recognize it. Operations and Insert are classes who implement IXmlSerializable, and the first one retrieves the schema sets of the inner types using a custom XmlSchemaProvider.

Am I doing something wrong? How can I help my ExampleSheet to accet the members of Operations? Should it ExampleSheet implement IXmlSerializable so I can build the reader and writer as I want, and would the schema be still useful?

Mattia Vitturi
  • 235
  • 4
  • 17
  • it's hard to follow you:) - simply put, do you want to reference one schema from another, is that it? – NSGaga-mostly-inactive Apr 11 '12 at 12:48
  • @NSGaga yes, from a different assembly. sorry for my poor explanation :) – Mattia Vitturi Apr 11 '12 at 12:50
  • does this help http://stackoverflow.com/questions/2357943/whats-the-difference-between-xsdinclude-and-xsdimport – NSGaga-mostly-inactive Apr 11 '12 at 12:52
  • @NSGaga not really, I've tried both xs:import and xs:include and actually they are in the same namespace. I omitted them to make the schemas simpler to be read. – Mattia Vitturi Apr 11 '12 at 12:55
  • Don't know if this is compatible with `XmlSerialization`, but have you looked into the `XmlSchemaSet` class instead of `XmlSchema`. You could then add your `ExampleSheet` and `Operations` schemas to the set, and you should have access to all elements in each. – psubsee2003 Apr 11 '12 at 13:47
  • @psubsee2003 Yes! I was trying that and it seems to work. :) If you suggest me that as an actual answer, I'll mark it as correct one. – Mattia Vitturi Apr 11 '12 at 13:59

1 Answers1

2

Instead of XmlSchema, have you looked into the XmlSchemaSet class?

I have not done a lot with XML Serialization, so I don't know if it will fit your current application, but I've used it before in a similar situation where I have to refer to types defined in 3 separate schemas.

The complete XmlSchemaSet object will then have access to all of the types in each of the schemas.

psubsee2003
  • 8,563
  • 8
  • 61
  • 79