in

SharePoint Blogs

The Best Place for SharePoint-related Blogs

MOSS 2007 and Software Product Lines

Hi. My name is Alfred de Weerd. In this blog I will deal with MOSS 2007 and development professionalisation in general. I'm planning to write about subjects like a MOSS product line in general, reusable assets, software factories, processes and the like.

Validating Features against wss.xsd

When looking on the net for information about validating features against their XSD, I could not find information on how to do this. Now this might be so obvious that nobody takes the bother. Or the existence of the schema files for Sharepoint 2007 is just related to its use on intellisense in Visual Studio, because it is not perfect.

 

Since I found validating the features not that obvious, and it proved very usefull in the MOSS Feature Generator , I'll show below how it is done.

 

First, I tried the XmlValidatingReader, but this is obsolete (or at least the methods I used), and the XmlValidatingReader proved very very mild in its validation.

 

Next, I used an example of Rajdeep Kwatra using XmlReaderSettings. (http://rajkwatra.blogspot.com/2007/06/validating-xml-in-net.html), which I adjusted for the use with Sharepoint. Rajdeep has explained all the Xml bits quite nicely, so I concentrate on some Sharepoint aspects. You need to add the CoreDefinitions.xsd file to the xmlSchema, before the wss.xsd, or you'll get complaints that one of the core definitions cannot be found. Doing so, you'll find that from the Schema.xml of SpFields, unlegal attributes are produced for the field nodes for Lookup types and Person/users. These attributes are WebId, UserSelectionMode, UserSelectionScope and Version. Also, quite a large number of validation errors resulted from the validation of the schema.xml files. I yet have to look into this.

 

Below is the code I used. xmlPath is the path to the feature.xml, elements.xml or schema.xml file. xsdPath is the path to the folder containing the Sharepoint schema files, typically "C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\XML";

 

        public static void ValidateXml(string xmlPath)

        {

            currentXmlFile = xmlPath;

 

            XmlSchema xmlSchema = XmlSchema.Read(new XmlTextReader(xsdPath+"CoreDefinitions.xsd"),

                new ValidationEventHandler(validatingReader_ValidationEventHandler));

            XmlSchema xmlSchema2 = XmlSchema.Read(new XmlTextReader(xsdPath + "wss.xsd"),

                new ValidationEventHandler(validatingReader_ValidationEventHandler));

 

            XmlReaderSettings xmlReaderSettings = new XmlReaderSettings();

            xmlReaderSettings.ValidationType = ValidationType.Schema;

            xmlReaderSettings.Schemas.Add(xmlSchema);

            xmlReaderSettings.Schemas.Add(xmlSchema2);

            xmlReaderSettings.ValidationEventHandler += new ValidationEventHandler(validatingReader_ValidationEventHandler);

            using (XmlReader xmlReader = XmlReader.Create(new XmlTextReader(xmlPath),

                    xmlReaderSettings))

            {

                while (xmlReader.Read()) ;

            }

        }

 

        static void validatingReader_ValidationEventHandler(object sender, ValidationEventArgs e)

        {

            validationErrors.Add(new MFGValidationError(e,currentXmlFile));

        }

 

I used this code in the MOSS Feature Generator, so that, when you generate features, you can inspect if there are Schema validation errors.  

Alfred

 

 

Comments

No Comments

Leave a Comment

(required )  
(optional )
(required )  
Add

Need SharePoint Training? Attend a SharePoint Bootcamp!

Posts (c) their respective authors. Everything else (c) 2007 SharePoint Experts