CSharp examples for System.Xml:XML Element
Get all elements from the XML schema.
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 . ja v a 2 s . c om*/ public class Main{ public static List<XmlSchemaElement> GetAllElements(XmlSchemaObject obj, bool recursive, List<XmlSchemaElement> allElements) { List<XmlSchemaElement> elements = new List<XmlSchemaElement>(); // Element if (obj.GetType().Equals(typeof(XmlSchemaElement))) { XmlSchemaElement element = (XmlSchemaElement)obj; XmlSchemaComplexType complexType = element.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) { elements = GetElements(childElement, elements, 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) { elements = GetElements(childElement, elements, recursive, allElements); } } #endregion } } if (obj.GetType().Equals(typeof(XmlSchemaComplexType))) { XmlSchemaComplexType complexType = obj 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) { elements = GetElements(childElement, elements, 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) { elements = GetElements(childElement, elements, recursive, allElements); } } #endregion } } if (obj is XmlSchemaGroup) { XmlSchemaGroup group = obj as XmlSchemaGroup; #region sequence /// Get the sequence particle of the complex type. XmlSchemaSequence sequence = group.Particle as XmlSchemaSequence; if (sequence != null) { // Iterate over each XmlSchemaElement in the Items collection. foreach (XmlSchemaObject childElement in sequence.Items) { elements = GetElements(childElement, elements, recursive, allElements); } } #endregion #region choice // check if it is e choice XmlSchemaChoice choice = group.Particle as XmlSchemaChoice; if (choice != null) { // Iterate over each XmlSchemaElement in the Items collection. foreach (XmlSchemaObject childElement in choice.Items) { elements = GetElements(childElement, elements, recursive, allElements); } } #endregion } //if (obj is XmlSchemaSequence) //{ // // Iterate over each XmlSchemaElement in the Items collection. // foreach (XmlSchemaObject childElement in ((XmlSchemaSequence)obj).Items) // { // elements = GetAllElements(childElement, recursive, allElements); // } //} //if (obj is XmlSchemaChoice) //{ // // Iterate over each XmlSchemaElement in the Items collection. // foreach (XmlSchemaObject childElement in ((XmlSchemaChoice)obj).Items) // { // elements = GetAllElements(childElement, recursive, allElements); // } //} return elements; } /// <summary> /// Get all elements from the schema. /// </summary> /// <param name="schema"></param> /// <returns></returns> public static List<XmlSchemaElement> GetAllElements(XmlSchema schema) { List<XmlSchemaElement> elements = new List<XmlSchemaElement>(); foreach (XmlSchemaElement item in schema.Elements.Values) { if (item is XmlSchemaElement) { Debug.WriteLine(" "); Debug.WriteLine("__________________________"); Debug.WriteLine(item.Name); Debug.WriteLine("---------------------------------"); //elements.Add((XmlSchemaElement)item); elements = GetElements((XmlSchemaElement)item, elements, true, new List<XmlSchemaElement>()); } } return elements; } }