I could partially solve the problem but I am facing other problem located in Error region in my code .
Please if you could find out where is the bug here
using System;
using System.Collections;
using System.IO;
using System.Xml;
using System.Xml.Schema;
namespace UnderstandingSOM
{
class Program
{
public static ArrayList ls;
static void Main(string[] args)
{
ls = new ArrayList();
TraverseSchema("book.xsd", ls);
Console.WriteLine("print ls");
#region Error Comments
#endregion
PrintVlaue(ls);
Console.ReadLine();
}
private static void PrintVlaue(IEnumerable myList)
{
foreach (Object obj in myList)
{
Console.WriteLine(obj);
}
}
private static XmlSchema ReadAndCompileSchema(string fileName)
{
XmlTextReader tr = new XmlTextReader(fileName, new NameTable());
try
{
try
{
XmlSchema schema = XmlSchema.Read(tr, new ValidationEventHandler(ValidationSchema));
tr.Close();
XmlSchemaSet xset = new XmlSchemaSet();
xset.Add(schema);
xset.ValidationEventHandler += new ValidationEventHandler(ValidationSchema);
xset.Compile();
return schema;
}
catch (XmlException xmlEx)
{
Console.WriteLine("Directory not found: " + xmlEx.Message);
return null;
}
}
catch (DirectoryNotFoundException dirEx)
{
Console.WriteLine("Directory not found: " + dirEx.Message);
return null;
}
}
private static void ValidationSchema(object sender, ValidationEventArgs args)
{
Console.WriteLine("Exception Severity: " + args.Severity);
Console.WriteLine(args.Message);
}
private static void TraverseSchema(string xsfFilename, ArrayList ls)
{
XmlSchema custSchema = ReadAndCompileSchema(xsfFilename);
try
{
foreach (XmlSchemaElement elem in custSchema.Elements.Values)
{
ProcessElement(elem, ls);
}
}
catch (NullReferenceException nRefEx)
{
Console.WriteLine("Value not found: " + nRefEx.Message);
}
}
private static ArrayList ProcessElement(XmlSchemaElement elem, ArrayList ls)
{
if (elem.ElementSchemaType is XmlSchemaComplexType)
{
Console.WriteLine("Object\"Complex Element\" : {0}", elem.Name);
ls.Add(elem.Name);
Console.WriteLine("test ls" + ls.Count);
}
else
{
Console.WriteLine("Basic Element : {0}", elem.Name);
ls.Add(elem);
Console.WriteLine("test ls for basic.. " + ls.Count);
}
if (elem.ElementSchemaType is XmlSchemaComplexType)
{
XmlSchemaComplexType ct = elem.ElementSchemaType as XmlSchemaComplexType;
ProcessSchemaObject(ct.ContentTypeParticle,ls);
}
return ls;
}
private static ArrayList ProcessSequence(XmlSchemaSequence sequence, ArrayList ls)
{
ProcessItemCollection(sequence.Items,ls);
return ls;
}
private static ArrayList ProcessChoice(XmlSchemaChoice choice, ArrayList ls)
{
ProcessItemCollection(choice.Items, ls);
return ls;
}
private static ArrayList ProcessAll(XmlSchemaAll All, ArrayList ls)
{
ProcessItemCollection(All.Items, ls);
return ls;
}
private static ArrayList ProcessExtension(XmlSchemaComplexContentExtension extension, ArrayList ls)
{
Console.WriteLine("Generalization Relationship : Extension");
return ls;
}
private static ArrayList ProcessItemCollection(XmlSchemaObjectCollection objs, ArrayList ls)
{
foreach (XmlSchemaObject obj in objs)
ProcessSchemaObject(obj, ls);
return ls;
}
private static ArrayList ProcessSchemaObject(XmlSchemaObject obj, ArrayList ls)
{
if (obj is XmlSchemaElement)
ProcessElement(obj as XmlSchemaElement, ls);
if (obj is XmlSchemaChoice)
ProcessChoice(obj as XmlSchemaChoice,ls);
if (obj is XmlSchemaSequence)
ProcessSequence(obj as XmlSchemaSequence, ls);
if (obj is XmlSchemaAll)
ProcessAll(obj as XmlSchemaAll, ls);
if (obj is XmlSchemaComplexContentExtension)
ProcessExtension(obj as XmlSchemaComplexContentExtension, ls);
return ls;
}
}
}