遍列schema代码

Code
 1class XmlSchemaTraverseExample
 2{
 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 XmlSchemaSet
14        // 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 collection
28        // 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}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值