Code 1class XmlSchemaTraverseExample 2{ 3staticvoid Main() 4{ 5// Add the customer schema to a new XmlSchemaSet and compile it. 6// Any schema validation warnings and errors encountered reading or 7// compiling the schema are handled by the ValidationEventHandler delegate. 8 XmlSchemaSet schemaSet =new XmlSchemaSet(); 9 schemaSet.ValidationEventHandler +=new ValidationEventHandler(ValidationCallback); 10 schemaSet.Add("http://www.ecidh.com/szeport/BLC_CHARGEUPIN", "c:\\blcchargeupin.xsd"); 11 schemaSet.Compile(); 12 13// Retrieve the compiled XmlSchema object from the XmlSchemaSet 14// by iterating over the Schemas property. 15 XmlSchema customerSchema =null; 16foreach (XmlSchema schema in schemaSet.Schemas()) 17{ 18 customerSchema = schema; 19 } 20foreach (XmlSchemaElement element in customerSchema.Elements.Values) 21{ 22 Console.WriteLine("{1}Element: {0}", element.Name, "\t"); 23 ReadElement(element,0); 24 25 } 26 Console.Read(); 27// Iterate over each XmlSchemaElement in the Values collection 28// of the Elements property. 29 30 } 31staticvoid ReadElement(XmlSchemaElement element,int i) 32{ 33 i++; 34string t ="\t"; 35for (int l =0; l < i; l++) 36{ 37 t +="\t"; 38 } 39 40 41// Get the complex type of the Customer element. 42 XmlSchemaComplexType complexType = element.ElementSchemaType as XmlSchemaComplexType; 43 44// If the complex type has any attributes, get an enumerator 45// and write each attribute name to the console. 46if (complexType.AttributeUses.Count >0) 47{ 48 IDictionaryEnumerator enumerator = 49 complexType.AttributeUses.GetEnumerator(); 50 51while (enumerator.MoveNext()) 52{ 53 XmlSchemaAttribute attribute = 54 (XmlSchemaAttribute)enumerator.Value; 55 56 Console.WriteLine("{1}Attribute: {0}", attribute.Name,t); 57 } 58 } 59 60// Get the sequence particle of the complex type. 61 XmlSchemaSequence sequence = complexType.ContentTypeParticle as XmlSchemaSequence; 62 63// Iterate over each XmlSchemaElement in the Items collection. 64foreach (XmlSchemaElement childElement in sequence.Items) 65{ 66 Console.WriteLine("{1}Element: {0}", childElement.Name,t); 67if (childElement.ElementSchemaType is XmlSchemaComplexType) 68{ 69 ReadElement(childElement, i); 70 } 71 72 } 73 } 74staticvoid ValidationCallback(object sender, ValidationEventArgs args) 75{ 76if (args.Severity == XmlSeverityType.Warning) 77 Console.Write("WARNING: "); 78elseif (args.Severity == XmlSeverityType.Error) 79 Console.Write("ERROR: "); 80 81 Console.WriteLine(args.Message); 82 } 83}