XML Schema Validator

This is extremely trivial XML Schema Validator; I just needed to verify my XML against a Schema File. Very simple code but maybe helpful to someone.

using System;

using System.Xml;

using System.IO;

using System.Xml.Schema;

namespace XmlConsole

{

/// <summary>

/// Summary description for Class1.

/// </summary>

class Class1

      {

private static int errors = 0;

/// <summary>

/// The main entry point for the application.

/// </summary>

            [STAThread]

static void Main(string[] args)

            {

try

                  {

// The Source XML File

                        XmlTextReader xtr = new XmlTextReader(“C:\\devresult.xml”);

                        XmlValidatingReader reader = new XmlValidatingReader(xtr);

                        reader.ValidationType = ValidationType.Schema;

// The Path of the XML Schema File against which the Validation is to be made.

// you can add more than one Files by using reader.Schemas.Add()

                        reader.Schemas.Add(null,new XmlTextReader(@”E:\Schema.xsd”));

                        reader.ValidationEventHandler += new ValidationEventHandler(reader_ValidationEventHandler);

while(reader.Read());

                        xtr.Close();

                        reader.Close();

if(errors == 0)

                              System.Console.WriteLine(“Validation Successfull”);

else

                              System.Console.WriteLine(“Validation Failed”);

                        System.Console.ReadLine();

                  }

catch(Exception exp)

                  {

                        System.Console.WriteLine(exp.Message);

                        errors ++;

                  }

            }

private static void reader_ValidationEventHandler(object sender, ValidationEventArgs e)

            {

                  System.Console.WriteLine(e.Message);

            }

      }

}

Advertisements

Parameter Passing Quiz :)

Ok, again posting after a long time.

Predict the output of the following code, the contains 3 sets of function calls, each set contains one call by value and one by reference, but the object being passed everytime is a reference type. It is recommended that you do not run the code, just predict the output.

StringBuilder used in the code is a class and is a reference type.

Also explain your answers.

class Class1

{

static void Main(string[] args)

{                

            StringBuilder str;

// Set 1

            str = new StringBuilder(“Types”);

            CrazyFunction1(str);

            Console.WriteLine(str.ToString());

            str = new StringBuilder(“Types”);

            CrazyFunction2(ref str);

            Console.WriteLine(str.ToString());

// Set 2

            str = new StringBuilder(“Types”);

            CrazyFunction3(str);

            Console.WriteLine(str.ToString());

            str = new StringBuilder(“Types”);

            CrazyFunction4(ref str);

            Console.WriteLine(str.ToString());

//Set 3

            str = new StringBuilder(“Types”);

            CrazyFunction5(str);

            Console.WriteLine(str.ToString());

            str = new StringBuilder(“Types”);

            CrazyFunction6(ref str);

            Console.WriteLine(str.ToString());

            Console.ReadLine();

      }

private static void CrazyFunction1(StringBuilder x)

      {

            x = null;

      }

private static void CrazyFunction2(ref StringBuilder x)

{

            x = null;

      }

private static void CrazyFunction3(StringBuilder x)

      {

            x.Append(” are crazy”);

      }

private static void CrazyFunction4(ref StringBuilder x)

      {

            x.Append(” are crazy”);

      }

private static void CrazyFunction5(StringBuilder x)

      {

            x = new StringBuilder(“crazy”);

      }

private static void CrazyFunction6(ref StringBuilder x)

      {

            x = new StringBuilder(“crazy”);

      }

}