Get Elements from XmlSchemaObject - CSharp System.Xml

CSharp examples for System.Xml:XML Element

Description

Get Elements from XmlSchemaObject

Demo Code


using System.Xml.Schema;
using System.Xml;
using System.Text;
using System.Linq;
using System.Diagnostics;
using System.Collections.Generic;
using System.Collections;
using System;/* w w w  . j av a  2  s  .  co  m*/

public class Main{
        private static List<XmlSchemaElement> GetElements(XmlSchemaObject element, List<XmlSchemaElement> list, bool recursive, List<XmlSchemaElement> allElements)
        {
            // Element
            if (element.GetType().Equals(typeof(XmlSchemaElement)))
            {
                XmlSchemaElement child = (XmlSchemaElement)element;

                if (child.Name != null)
                {
                    if (list.Where(e => e.Name.Equals(child.Name)).Count() == 0)
                    {
                        //if (!child.SchemaTypeName.Name.Equals(parentTypeName))
                        //{
                        
                        list.Add(child);

                        if (recursive)
                        {

                            Debug.WriteLine("--<" + child.Name);

                            XmlSchemaComplexType complexType = child.ElementSchemaType as XmlSchemaComplexType;
                            if (complexType != null)
                            {
                                #region sequence

                                /// Get the sequence particle of the complex type.
                                XmlSchemaSequence sequence = complexType.ContentTypeParticle as XmlSchemaSequence;
                                if (sequence != null)
                                {
                                    // Iterate over each XmlSchemaElement in the Items collection.
                                    foreach (XmlSchemaObject childElement in sequence.Items)
                                    {
                                        list = GetElements(childElement, list, recursive, allElements);
                                    }
                                }

                                #endregion

                                #region choice

                                // check if it is e choice
                                XmlSchemaChoice choice = complexType.ContentTypeParticle as XmlSchemaChoice;
                                if (choice != null)
                                {
                                    // Iterate over each XmlSchemaElement in the Items collection.
                                    foreach (XmlSchemaObject childElement in choice.Items)
                                    {
                                        list = GetElements(childElement, list, recursive, allElements);
                                    }
                                }

                                #endregion
                            }
                        }

                        //}
                    }
                }
                else
                {
                    if (child.RefName != null)
                    {
                        XmlSchemaElement refElement = allElements.Where(e => e.QualifiedName.Equals(child.RefName)).FirstOrDefault();
                        if (refElement != null)
                        {
                            list.Add(refElement);

                            if (recursive)
                            {

                                Debug.WriteLine("--<" + refElement.Name);

                                XmlSchemaComplexType complexType = refElement.ElementSchemaType as XmlSchemaComplexType;
                                if (complexType != null)
                                {
                                    #region sequence
                                    /// Get the sequence particle of the complex type.
                                    XmlSchemaSequence sequence = complexType.ContentTypeParticle as XmlSchemaSequence;
                                    if (sequence != null)
                                    {
                                        // Iterate over each XmlSchemaElement in the Items collection.
                                        foreach (XmlSchemaObject childElement in sequence.Items)
                                        {
                                            list = GetElements(childElement, list, recursive, allElements);
                                        }
                                    }

                                    #endregion

                                    #region choice
                                    // check if it is e choice
                                    XmlSchemaChoice choice = complexType.ContentTypeParticle as XmlSchemaChoice;
                                    if (choice != null)
                                    {
                                        // Iterate over each XmlSchemaElement in the Items collection.
                                        foreach (XmlSchemaObject childElement in choice.Items)
                                        {
                                            list = GetElements(childElement, list, recursive, allElements);
                                        }
                                    }
                                    #endregion
                                }
                            }
                        }
                    }
                }
            }
            else
                if (element.GetType().Equals(typeof(XmlSchemaChoice)))
            {
                XmlSchemaChoice choice = (XmlSchemaChoice)element;

                // Iterate over each XmlSchemaElement in the Items collection.
                foreach (XmlSchemaObject childElement in choice.Items)
                {
                    list = GetElements(childElement, list, recursive, allElements);
                }
            }
            else 
                if (element.GetType().Equals(typeof(XmlSchemaSequence)))
            {
                XmlSchemaSequence sequence = (XmlSchemaSequence)element;
                    
                // Iterate over each XmlSchemaElement in the Items collection.
                foreach (XmlSchemaObject childElement in sequence.Items)
                {
                    list = GetElements(childElement, list, recursive, allElements);
                }
            }

            return list;
        }
}

Related Tutorials