Monthly Archives: June 2010

Class to Validate XML Against XSD

Recently i was involved in programming generic interface for Submitting PO. I came across the XSD concept of programming. I used the XSD to validate xml passed from different sub system and serialize the data.

Below is the sample code to validate the Xml against Xsd

/////////////////////////////

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Schema;
using System.Xml;
using System.IO;

namespace XSDValidation
{
    public static class POXsdValidation
    {
        private static string ErrorMessage;

public enum XmlType
    {
        XSD1,
        XSD2
    }

        public static string XsdError
        {
            get
            {
                return ErrorMessage;
            }
        }
        /// <summary>
        /// Validates the xml string against the xsd
        /// </summary>
        /// <param name="InputXml">Xml String contains the full xml</param>
        /// <param name="XsdPath">Xsd Path to be validated</param>
        private static void ValidateXmlByPath(string InputXml,string XsdPath)
        {
            StringReader xmlString = new StringReader(InputXml);
            ////StringReader xsdReader = new StringReader(XsdPath);
            ErrorMessage = string.Empty;
            ////XmlReader XsdReader = XmlReader.Create(xsdReader);

            //// Create the XmlSchemaSet class.
            XmlSchemaSet sc = new XmlSchemaSet();

            //// Add the schema to the collection.
            sc.Add(null, XsdPath);

            //// Set the validation settings.
            XmlReaderSettings settings = new XmlReaderSettings();
            settings.ValidationType = ValidationType.Schema;
            settings.Schemas = sc;
            settings.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);

            //// Create the XmlReader object.
            XmlReader reader = XmlReader.Create(xmlString, settings);

            //// Parse the file.
            while (reader.Read())
            {
            }
        }
        /// <summary>
        /// Validates the xmlreader content against xsd
        /// </summary>
        /// <param name="InputXml">Xml Reader</param>
        /// <param name="XsdPath">XSD Path</param>
        private static void ValidateXml(XmlReader InputXml, string XsdPath)
        {
            ////StringReader xmlString = new StringReader(InputXml);
            ////StringReader xsdReader = new StringReader(XsdPath);
            ErrorMessage = string.Empty;
            ////XmlReader XsdReader = XmlReader.Create(xsdReader);

            //// Create the XmlSchemaSet class.
            XmlSchemaSet sc = new XmlSchemaSet();

            //// Add the schema to the collection.
            sc.Add(null, XsdPath);

            //// Set the validation settings.
            XmlReaderSettings settings = new XmlReaderSettings();
            settings.ValidationType = ValidationType.Schema;
            settings.Schemas = sc;
            settings.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);

            //// Create the XmlReader object.
            XmlReader reader = XmlReader.Create(InputXml, settings);

            //// Parse the file.
            while (reader.Read())
            {
            }
        }
        /// <summary>
        /// Validates the Xml Against xsd
        /// </summary>
        /// <param name="InputXml">Input Xml String to Validate</param>
        /// <param name="InputType">Type of Validation</param>
        public static void ValidateXml(string InputXml,XmlType InputType)
        {
            string PathWCF = System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath;
            //// Add the schema to the collection.
            switch (InputType)
            {
                case XML1:
                    PathWCF += "\\bin\\schemas\\XSD1.xsd";
                    break;
                case XML2:
                    PathWCF += "\\bin\\schemas\\XSD2.xsd";
                    break;
            }

            ValidateXmlByPath(InputXml, PathWCF);
        }
        /// <summary>
        /// Callback function for Validate Schema
        /// </summary>
        /// <param name="sender">Object value</param>
        /// <param name="e">Validation message object</param>
        private static void ValidationCallBack(object sender, ValidationEventArgs e)
        {
            ErrorMessage += "<Validation>" +  e.Message + "</Validation>" ;
        }
    }
}

WCF DataService with Custom Function and parameter

namespace Senthamil.Sample

{
    public class PORSS : DataService<ModelEntities>
    {
        // This method is called only once to initialize service-wide policies.
        public static void InitializeService(DataServiceConfiguration config)
        {
            // TODO: set rules to indicate which entity sets and service operations are visible, updatable, etc.
            // Examples:
            config.SetEntitySetAccessRule(“RSS_STATUS”, EntitySetRights.AllRead);
            config.SetServiceOperationAccessRule(“MyFunction”, ServiceOperationRights.All);
            config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
        }

        [WebGet]
        public IQueryable<RSS_STATUS> MyFunction(string RefNumb)
        {
            ModelEntities MySource = new ModelEntities();
            return from c in MySource.RSS_STATUS
                   where c.SUB_SYSTEM_NUMBER == RefNumb
                   select c;
        }
    }
}

This version of SQL Server in use does not support datatype datetime2

I got this strange issue today, and was struggling to fix this. This issue is related to the SQL 2008 and 2005 compatibility. I had a local machine with SQL 2008, but the Dev environment is with SQL 2005. When i generated the Entities using EF in my local machine it had marked it with 2008 version. When deployed to 2005 the issue was reported.

To fix:

Open the .edmx file using the XML editor option and look for the attribute ProviderManifestToken=”2008″ and change it to 2005 and save. Build and deploy it works.

image