Click here to Skip to main content
15,899,937 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to display all the elements of XSD what is wrong in my code it just show first element

            XmlSchema xmlschema = ReadAndCompileSchema(xsdFilePath);
            
            foreach (XmlSchemaElement xmlschemaelement in xmlschema.Elements.Values)
            {
                ls.Add(xmlschemaelement.Name);
                ProcessElement(xmlschemaelement,ls);//, mdList*/);
            }

            foreach (string l in ls)
            {
                textBox1.Text = l;
            }
            
        }
        public List<string> ProcessElement(XmlSchemaElement elem, List<string> ls)
        {
            
            if (elem.ElementSchemaType is XmlSchemaComplexType)
            {
                ls.Add(elem.Name);
                XmlSchemaComplexType ct = elem.ElementSchemaType as XmlSchemaComplexType;
            
                //checking if the complextype element has attributes or any of its base types 
                //has attributes and add it to the attribute list
                foreach (DictionaryEntry obj in ct.AttributeUses)
                {
                   ls.Add((obj.Value as XmlSchemaElement).Name);
                }

                 ProcessSchemaObject(ct.ContentTypeParticle, ls);
           }

           else if (elem.ElementSchemaType is XmlSchemaSimpleType)
            {
                ls.Add(elem.Name);
            }

            return ls;

        }
        public List<string> ProcessSchemaObject(XmlSchemaObject obj, List<string> ls)
                {
                    
                    //by calling ProcessElement
                    if (obj is XmlSchemaElement)
                    {
                        ProcessElement(obj as XmlSchemaElement, ls);
                        return ls;
                    }

         
                    
                    return ls;
                } 

               
    }
}
Posted
Updated 4-Jan-11 23:08pm
v2

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;// = new ArrayList();
        
        static void Main(string[] args)
        {
            ls = new ArrayList();
            TraverseSchema("book.xsd", ls);
            Console.WriteLine("print ls");
            #region Error Comments
            /*
             * I donot know why here the result from the arraylist show me 
             * books
             * book
             * system.xml.schema.xmlschemaelement; which is instead of element name author , title.. so on
             * system.xml.schema.xmlschemaelement;
             * system.xml.schema.xmlschemaelement;
             * system.xml.schema.xmlschemaelement;
             */
            #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());
            // The Read method will throw errors encountered
            // on parsing the schema
            try
            {
                try
                {
                    XmlSchema schema = XmlSchema.Read(tr, new ValidationEventHandler(ValidationSchema));
                    tr.Close();
                    XmlSchemaSet xset = new XmlSchemaSet();
                    xset.Add(schema);
                    xset.ValidationEventHandler += new ValidationEventHandler(ValidationSchema);
                    // The Compile method will throw errors encountered on compiling the schema
                    xset.Compile();
                    return schema;
                }
                catch (XmlException xmlEx)
                {
                    // Let the user know that error happened during the validation.
                    Console.WriteLine("Directory not found: " + xmlEx.Message);
                    return null;
                }
            }
            catch (DirectoryNotFoundException dirEx)
            {
                // Let the user know that the directory did not exist.
                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)
            {
                // Let the user know that you try to reference an object in your code that does not exist.
                Console.WriteLine("Value not found: " + nRefEx.Message);
            }
        }
        private static ArrayList ProcessElement(XmlSchemaElement elem, ArrayList ls)
        {
            //Determine the complex nodes on XTree
            if (elem.ElementSchemaType is XmlSchemaComplexType)
            {
                Console.WriteLine("Object\"Complex Element\" : {0}", elem.Name);
                ls.Add(elem.Name);
                Console.WriteLine("test ls" + ls.Count);
 
            }
            else
            {
                //Determine the Basic nodes on XTree
                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;
        }
    }
}
 
Share this answer
 
Comments
ninasg11 5-Jan-11 4:41am    
up
I can see just one bug so far. If you fix it but it does not solve your problem, please report back.

C#
foreach (string l in ls)
{
    textBox1.Text = l;
}


Let's assume ls is calculated correctly. You probably want to show whole collection (List<string> ls) in textBox1. Instead, you just change the Text ls.Count times, each time replacing previous value of Text.

You need to do something like that instead:

C#
using StringBuilder = System.Text.StringBuilder;

//...

StringBuilder sb = new StringBuilder(); //empty 
foreach (string line in ls) {
    sb.Append(line);
    sb.Append("\r\n");
}
textBox1.Text = sb.ToString();
 
Share this answer
 
Comments
ninasg11 4-Jan-11 23:27pm    
Actually, the problem was in displaying all the xsd elements. my code just display the first elements which books but when I use StringBuilder it shows me double words of books
books
books

which is not what I want
anyway, Thank you for your effort
Sergey Alexandrovich Kryukov 5-Jan-11 14:30pm    
Nevertheless, I just pointed out your bug. Without the fix, you will not show your string data, first elements or whatever.

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