遍列schema代码
1
class XmlSchemaTraverseExample2


{3
static void 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 XmlSchemaSet14
// by iterating over the Schemas property.15
XmlSchema customerSchema = null;16
foreach (XmlSchema schema in schemaSet.Schemas())17

{18
customerSchema = schema;19
}20
foreach (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 collection28
// of the Elements property.29
30
}31
static void ReadElement(XmlSchemaElement element,int i)32

{33
i++;34
string t = "\t";35
for (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.46
if (complexType.AttributeUses.Count > 0)47

{48
IDictionaryEnumerator enumerator =49
complexType.AttributeUses.GetEnumerator();50

51
while (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.64
foreach (XmlSchemaElement childElement in sequence.Items)65

{66
Console.WriteLine("{1}Element: {0}", childElement.Name,t);67
if (childElement.ElementSchemaType is XmlSchemaComplexType)68

{69
ReadElement(childElement, i);70
}71

72
}73
}74
static void ValidationCallback(object sender, ValidationEventArgs args)75

{76
if (args.Severity == XmlSeverityType.Warning)77
Console.Write("WARNING: ");78
else if (args.Severity == XmlSeverityType.Error)79
Console.Write("ERROR: ");80

81
Console.WriteLine(args.Message);82
}83
}

浙公网安备 33010602011771号