posts - 78, comments - 79, trackbacks - 84

My Links

News

View Anthony Trudeau's profile on LinkedIn

Add to Technorati Favorites

Article Categories

Archives

Post Categories

Other Links

Using XML Schemas as Embedded Resources for Serialization

Keith Pijanowski had a great article in MSDN magazine titled “Enrich Your XML Serialization With Schema Providers in the .NET Framework.”  His article talks about the use of XML schemas for the purpose of serialization with classes.  Using an XML schema removes some of the limitations I mentioned in a previous article about using XML serialization including the need for a set accessor.

This of course brings a new problem to the table.  Where do you put the schema files?  The most obvious choice is as content in separate XSD files.  That, however, is that many more files to worry about during deployment.  Another choice is including the XSD files as embedded resources in an assembly.  This method allows you to keep the schema with the mapped content and easily use it in other places (e.g. web services) without having any additional knowledge of the schema's existence.

In order to use schema providers with your .NET 2.0 classes you need to implement the IXmlSerializable interface and then assign the XmlSchemaProvider attribute to the class.  The attribute takes a string that represents the name of a static method in your new class that will provide the schema.

I will not go into too much detail about the implementation since it's covered well in the MSDN article, but I will say that this method essentially needs to do two things:  return an XmlSchemaComplexType and add the schema to the schema set which is the sole argument for the static method.  With those two tasks in mind I created a helper class to do that work.  The first method is responsible for getting the schema.  And the second method is responsible from getting the complex type from the schema.

Here is the code for my helper class:

internal static class SchemaUtility
{
        public static XmlSchemaComplexType GetComplexType(XmlSchema schema, string typeName)
        {
            if (schema == null)
                throw new ArgumentNullException("schema");

            if (string.IsNullOrEmpty(typeName))
                throw new ArgumentNullException("typeName");

            XmlQualifiedName xmlName = new XmlQualifiedName(typeName);
            return (XmlSchemaComplexType)schema.SchemaTypes[xmlName];
        }

        public static XmlSchema GetSchema(string schemaName)
        {
            if (string.IsNullOrEmpty(schemaName))
                throw new ArgumentNullException("schemaName");

            Assembly thisAssembly = Assembly.GetExecutingAssembly();
            Stream xsdStream = thisAssembly.GetManifestResourceStream(schemaName);
            XmlSerializer serializer = new XmlSerializer(typeof(XmlSchema));
            XmlSchema schema = (XmlSchema)serializer.Deserialize(xsdStream);

            return schema;
        }
}

Very little needs to go on with my original class.  I can implement what I need within the static method referenced by the XmlSchemaProvider attribute and a few constants which I have embedded in the code for the purpose of the example.  Here is the static method:

        public static XmlSchemaComplexType GetSchemaProvider(XmlSchemaSet schemaSet)
        {
            if (schemaSet == null)
                throw new ArgumentNullException("schemaSet");

            XmlSchema schema = SchemaUtility.GetSchema(“MyNamespace.MyClass.xsd“);
            XmlSchemaComplexType type = SchemaUtility.GetComplexType(schema, “myclasstype“);

            schemaSet.Add(schema);
            return type;
        }

The first literal in the call to GetSchema is the fully-qualified name for the schema as referenced from embedded resources.  This name includes the full namespace, sub-folder for the schemas (if applicable; not shown), and the name of the schema file.  The second literal is the name of the complex type as defined in the schema file itself.

Using the SchemaUtility helper class will allow you to reuse the code across any number of assemblies that require the XML schemas and it also eliminates the need to manage separate schema files.

Print | posted on Wednesday, June 14, 2006 1:40 PM |

Feedback

Gravatar

# re: Using XML Schemas as Embedded Resources for Serialization

thanx for you. its greet article
7/17/2008 7:57 AM | Msn

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 5 and 6 and type the answer here:

Powered by: