zoukankan      html  css  js  c++  java
  • C#与XML Schema的问题

    http://bbs.csdn.net/topics/50493564

      用XmlSchema.Read方法读取了一个xsd文件,请问如何遍历检索器中的ComplexType与SimpleType?如何得到其中的所有元素?

    johnczy:

    你的“检索器”是指什么?有对应的英文吗?

    我想你大概是要查
    XmlSchemaElement class 
    XmlSchemaElement.ElementType property 


    XmlSchema schema = new XmlSchema(); 
    XmlSchemaElement element = new XmlSchemaElement(); 
    schema.Items.Add(element); 
    element.Name = "stringElementWithAnyAttribute"; 
    // <xs:complexType> 
    XmlSchemaComplexType complexType = new XmlSchemaComplexType(); 
    element.SchemaType = complexType; 

    if (element.SchemaType is XmlSchemaComplexType){
      .... 
    }

     
    参http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
    frlrfsystemxmlschemaxmlschemaobjecttableclasstopic.asp

    希望有所帮助!

    Zhiyong[MS]
    本贴子以"现状"提供且没有任何担保,同时也没有授予任何权利

    LZ:

    我现在就是读取了一个schema文件,想能够得到其中的所有simpletype和complextype,以此来对一个XML的格式进行约束。

    hds:

    XmlValidatingReader

    namespace HowTo.Samples.XML
    {

    using System;
    using System.IO;
    using System.Xml;
    using System.Xml.Schema;

    public class ValidationReadingXMLSample
    {
        private const String document1 = "booksDTD.xml";
        private const String document2 = "booksDTDFail.xml";
        private const String document3 = "booksSchema.xml";
        private const String document4 = "booksSchemaFail.xml";
        private const String document5 = "schema.xsd";

        private XmlValidatingReader myXmlValidatingReader = null;
        private XmlTextReader myXmlTextReader = null;
        private Boolean Success = true;

        public static void Main()
        {
            String[] args = {document1, document2, document3, document4, document5};
            ValidationReadingXMLSample myValidationReadingXMLSample = new ValidationReadingXMLSample();
            myValidationReadingXMLSample.Run(args);
        }

        public void Run(String[] args)
        {
            try
            {
               // 用 DTD 验证 XML 文件
                Console.WriteLine();
                Console.WriteLine("正在用 DTD 文件 books.dtd 验证 XML 文件 booksDTD.xml ...");
                myXmlTextReader = new XmlTextReader (args[0]);
                myXmlValidatingReader = new XmlValidatingReader(myXmlTextReader);
                myXmlValidatingReader.ValidationType = ValidationType.DTD;
                Validate();

                // DTD 验证失败
                Success = true;
                Console.WriteLine();
                Console.WriteLine("正在用 DTD 文件 books.dtd 验证 XML 文件 booksDTDFail.xml ...");
                myXmlTextReader = new XmlTextReader (args[1]);
                myXmlValidatingReader = new XmlValidatingReader(myXmlTextReader);
                myXmlValidatingReader.ValidationType = ValidationType.DTD;
                Validate();

                XmlSchemaCollection myXmlSchemaCollection = new XmlSchemaCollection();
                myXmlSchemaCollection.Add("schema.xsd" , new XmlTextReader(args[4]));

                // 用架构验证 XML 文件
                Success = true;
                Console.WriteLine();
                Console.WriteLine("正在用架构文件 schema.xsd 验证 XML 文件 booksSchema.xml ...");
                myXmlTextReader = new XmlTextReader (args[2]);
                myXmlValidatingReader = new XmlValidatingReader(myXmlTextReader);
                myXmlValidatingReader.Schemas.Add(myXmlSchemaCollection);
                myXmlValidatingReader.ValidationType = ValidationType.Schema;
                Validate();

                // 架构验证失败
                Success = true;
                Console.WriteLine();
                Console.WriteLine("正在用架构文件 schema.xsd 验证 XML 文件 booksSchemaFail.xml ...");
                myXmlTextReader = new XmlTextReader (args[3]);
                myXmlValidatingReader = new XmlValidatingReader(myXmlTextReader);
                myXmlValidatingReader.Schemas.Add(myXmlSchemaCollection);
                myXmlValidatingReader.ValidationType = ValidationType.Schema;
                Validate();
            }

            catch (Exception e)
            {
                Console.WriteLine("异常:" + e.ToString());
            }

            finally
            {
                // 通过 XmlTextReader 完成
                if (myXmlValidatingReader != null)
                    myXmlValidatingReader.Close();
            }
        }

        private void Validate()
        {
            try
            {
                // 设置验证事件处理程序
                myXmlValidatingReader.ValidationEventHandler += new ValidationEventHandler (this.ValidationEventHandle);

                // 读取 XML 数据
                while (myXmlValidatingReader.Read()){}
                Console.WriteLine ("验证已完成。验证 {0}", (Success==true ? "成功" : "失败"));
            }
            catch (XmlException e)
            {
                Console.WriteLine ("Xml 异常:" + e.ToString());
            }

            catch (Exception e)
            {
                Console.WriteLine ("异常:" + e.ToString());
            }
        }

        public void ValidationEventHandle (object sender, ValidationEventArgs args)
        {
            Success = false;

            Console.WriteLine(" 验证错误:" + args.Message);

            if (args.Severity == XmlSeverityType.Warning)
            {
                Console.WriteLine("未找到要强制验证的架构。");
            } else
                if (args.Severity == XmlSeverityType.Error)
                {
                       Console.WriteLine("验证实例文档时发生验证错误。");
                } 

            if (args.Exception != null) // XSD 架构验证错误
            {
                Console.WriteLine(args.Exception.SourceUri + "," +  args.Exception.LinePosition + "," +  args.Exception.LineNumber);
            }

            //if (myXmlValidatingReader.Reader.LineNumber > 0)
            //{
            //    Console.WriteLine("Line: "+ myXmlValidatingReader.Reader.LineNumber + " Position: " + myXmlValidatingReader.Reader.LinePosition);
            //}
        }

    }// 结束类 ValidationReadingXMLSample
    }// 结束 HowTo.Samples.XML

    LZ:

    我不仅仅是对XML文件进行检查,而且还要做很多事。

    我是把XML文件显示在一个GridTree结构(就是在Grid的左侧显示出XML文件的大纲结构)中。

    用在schema中定义的simpletype决定Grid中cell的属性,比如如果是枚举类型,那么就把cell设为combolist,其中填入在schema中定义的枚举备选;如果是日期型,那么cell就设为日期型……

    用在schema中定义的complextype决定大纲中的数据填/删的允许。

    所以这就要求我能够在读取XML文件后,能再读取schema文件,并得到所有的SimplyType和ComplexType。

    XmlSchema mySchema = new XmlSchema();
    XmlTextReader myReader = new XmlTextReader(@"c:Profile.xsd");
    mySchema = XmlSchema.Read(myReader, null);

    得到了schema文件,在mySchema.Items.Count中,我能得到现在的所有元素+type总数,在watch窗口 中,mySchema.Items.list中能看到所有的element ype,这个list在msdn中说是public的,但在程序中我无法访 问,也没找到什么方法能够访问。

    请问,有什么方法能解决我的问题?

    joh。。。:

    你是想做一个像VisualStudio 里的ShemaDesigner 那样的东西吗?

    Q: mySchema.Items.list中能看到所有的element ype,这个list在msdn中说是public的,但在程序中我无法访问,也没找到什么方法能够访问。
    A: mySchema.Items is a collection, there is no list property but you can access all items like this: 
       foreach(object o in mySchema.Items){
            ... 
       }

    Hope this would hlep.

    wis。。。:

    嗯,这个问题我早上已经解决了
    foreach (XmlSchemaObject schemaObject in mySchema.Items) {……}

    现在还有问题,比如NMTOKEN类型,我要把他的附属集合都找出来,现在在watch中能看到,但不知道怎么访问。

    不是做ShemaDesigner,而是利用schema的正则约束,对文件进行多种的检查。

    jos:

    Q: 比如NMTOKEN类型,我要把他的附属集合都找出来
    附属集合?你指的是 Facets 吗?

    A:你大概要自己做了,参考W3的标准 http://www.w3.org/XML/Group/xmlschema-current/datatypes/datatypes.html
      比如:
            private static string[] NMTOKENFacets = new string[] {"enumeration", "length", "maxLength", "minLength", "pattern", "whiteSpace", };

    LZ:

    我就是要找出MNTOKEN下的Facets。我有个字定义的simplytype,基类型是NMTOKEN,其中自己定义了一些枚举成员,南京、北京、上海……,现在我就是得到了这个SimplyType,但无法访问到其中这些自定的枚举成员。

    有没有人做过类似的事?

    LZ:

    遍历xsd文件可以的到每个simplet ype和complex type,可以得到其中的XmlSchemaSimpleTypeContent,但在BaseType为NMTOKEN的SimpleType的Content中无法得到他的Facets。

    在调试时,XmlSchemaSimpleTypeContent content在watch窗口中可以看到其下有
    [System.Xml.Schema.XmlSchemaSimpleTypeRestriction]
    System.Xml.Schema.XmlSchemaAnnotated

    其中我所要的Facets就在[System.Xml.Schema.XmlSchemaSimpleTypeRestriction]里,但我不知道怎 样访问这个[System.Xml.Schema.XmlSchemaSimpleTypeRestriction],而且我想知道这个[]是代表什么意 思?
    XmlSchemaSimpleTypeContent继承自XmlSchemaAnnotated,这个[……]中的东西和content是什么关系?怎么访问其中的内容?

  • 相关阅读:
    学习资料
    InstallShield常用工具
    InstallShield中调用API
    系统目录
    abort和exit
    Uninstall Registry Key
    GDI+资料
    VBScript是什么?有什么优缺点?
    DrawImage调查
    KEIL MDK环境下uCOSII在LPC17xx上的移植实例 Chung
  • 原文地址:https://www.cnblogs.com/joean/p/4982879.html
Copyright © 2011-2022 走看看