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}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix