Click here to Skip to main content
15,890,186 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
        foreach (XmlSchema schema1 in schemaSet.Schemas())
        {
            compiledSchema = schema1;
        }

        foreach (XmlSchemaObject schemaObject in compiledSchema.Items)
        {            
            //Console.WriteLine(schemaObject.GetType());
            if (schemaObject.GetType() == typeof(XmlSchemaElement))
            {
                XmlSchemaElement elementType = (XmlSchemaElement)schemaObject;
                Console.WriteLine("{0}{1}", "xmlschemaelement:", elementType.Name);
            }
            if (schemaObject.GetType() == typeof(XmlSchemaSimpleType))
            {
                XmlSchemaSimpleType simpleType = (XmlSchemaSimpleType)schemaObject;
                Console.WriteLine("{0} {1}", simpleType.Name, simpleType.Datatype.ValueType);
            }
            if (schemaObject.GetType() == typeof(XmlSchemaComplexType))
            {
                XmlSchemaComplexType complexType = (XmlSchemaComplexType)schemaObject;
                Console.WriteLine("{0}{1}", "xmlschemacomplexType:", complexType.Name);//, complexType.Datatype.ValueType);
                //ProcessElement(schemaObject as XmlSchemaElement);
                ProcessSchemaObject(schemaObject);
            }
            if (schemaObject.GetType() == typeof(XmlSchemaSequence)) { Console.WriteLine("insequence"); }
        }
        Console.ReadLine();
        xtr.Close();
    }
 
    private static void ProcessElement(XmlSchemaElement elem)
    {
        //Determine the complex nodes on XTree
        if (elem.ElementSchemaType is XmlSchemaComplexType)
        {
            Console.WriteLine("Object\"Complex Element\" : {0}", elem.Name);
            //Console.WriteLine("test ls" + ls.Count);
            XmlSchemaComplexType ct = elem.ElementSchemaType as XmlSchemaComplexType;
            ProcessSchemaObject(ct.ContentTypeParticle);
        }
        else
        {
            //Determine the Basic nodes on XTree
            Console.WriteLine("Basic Element : {0}", elem.Name);
            //Console.WriteLine("test ls for basic.. " + ls.Count);
        }
    }
 
    private static void ProcessSchemaObject(XmlSchemaObject obj)
    {
        if (obj is XmlSchemaElement)
            ProcessElement(obj as XmlSchemaElement);
        if (obj is XmlSchemaComplexType)
        {
            //XmlSchemaElement se = obj as XmlSchemaElement;
            Console.WriteLine("Debug : {0}", obj.GetType());
            ProcessElement(obj as XmlSchemaElement);
        }
 }
}

I face this error:
A NullReferenceException occurs when you try to reference an object in your code that does not exist. For example, you may have tried to use an object without using the New keyword first, or tried to use an object whose value is set to null
in this part of code
if (elem.ElementSchemaType is XmlSchemaComplexType)
{
  Console.WriteLine("Object\"Complex Element\" : {0}", elem.Name);
  //Console.WriteLine("test ls" + ls.Count);
  XmlSchemaComplexType ct = elem.ElementSchemaType as XmlSchemaComplexType;
  ProcessSchemaObject(ct.ContentTypeParticle);
}

It should not be a null value, that happened when it starts to parse complextype.
Posted
Updated 4-Jan-11 23:18pm
v2

You can try the following:
1. Set up the debugger to break when the exception is thrown.
2. Use the callstack to navigate between the methods
3. inspect the variables

Hope this helps

Regards
Espen Harlinn
 
Share this answer
 
An XmlSchemaComplexType cannot be cast as XmlSchemaElement.

if (obj is XmlSchemaComplexType)
        {
            //XmlSchemaElement se = obj as XmlSchemaElement;
            Console.WriteLine("Debug : {0}", obj.GetType());
            ProcessElement(obj as XmlSchemaElement);
        }

Your parameter elem in ProcessElement will be null.

Cheers
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900