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

weileily:

  用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("\t验证错误:" + 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\type,这个list在msdn中说是public的,但在程序中我无法访
问,也没找到什么方法能够访问。

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

joh。。。:

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

Q: mySchema.Items.list中能看到所有的element\type,这个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是什么关系?怎么访问其中的内容?

C#与XML Schema的问题的更多相关文章

  1. XML Schema and XMLspy notes

    Introduction An xml documents consists of elements, attributes and text. There are two structures in ...

  2. XSD(XML Schema Definition)用法实例介绍以及C#使用xsd文件验证XML格式

    XML Schema 语言也称作 XML Schema 定义(XML Schema Definition,XSD),作用是定义 XML 文档的合法构建模块,类似 DTD,但更加强大. 作用有: ①定义 ...

  3. XML Schema命名空间解析

    URI Web中汇集了各种资源.资源可以是具有标识的任何事物, 如文档. 文件. 菜单项. 计算机. 服务等, 甚至可以包括人. 组织和概念[BernersLee 1998].在Web体系结构中, ...

  4. 【转】XSD (xml Schema Definition)

    来自:http://www.cnblogs.com/newsouls/archive/2011/10/28/2227765.html Xml Schema的用途 1.  定义一个Xml文档中都有什么元 ...

  5. XML Schema的基本语法(转)

    XML Schema的基本语法(转) XSDL(XML Schema定义语言)由元素.属性.命名空间和XML文档种的其他节点构成的. 一.XSD中的元素 XSD文档至少要包含:schema根元素和XM ...

  6. Xml Schema:C#访问在complextype中插入新元素

    最近用c#写Xml Schema代码,找了很久也找不到如何在已有的complextype中插入新的element,最后我充分发挥自己的聪明才智,哈哈,终于从...中找到了灵感. XmlSchemaSe ...

  7. [BTS] System.Xml.Schema.XmlSchemaException: The complexType has already been declared when generate IDoc schema.

    I use wcf-sap adapter for generate the schema of IDoc that named "YHREMPMASTER". but throw ...

  8. onfiguration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/security]

    org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Una ...

  9. dubbo源码之一——xml schema扩展

    dubbo源码版本:2.5.4 dubbo-parent |----dubbo-config |----dubbo-config-api |----com.alibaba.dubbo.config.* ...

随机推荐

  1. MVC 中aspx的增删改查

    先看总体结构 LInQ #pragma warning disable 1591 //--------------------------------------------------------- ...

  2. HR数据抽取:通过 Read Master Data 转换规则读取时间相关主属性数据

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  3. 《BI那点儿事》三国数据分析系列——蜀汉五虎上将与魏五子良将武力分析,绝对的经典分析

    献给广大的三国爱好者们,希望喜欢三国的朋友一起讨论,加深对传奇三国时代的了解 数据分析基础概念:集中趋势分析是指在大量测评数据分布中,测评数据向某点集中的情况.总体(population)是指客观存在 ...

  4. LTE Module User Documentation(翻译8)——核心网(EPC)

    LTE用户文档 (如有不当的地方,欢迎指正!) 14 Evolved Packet Core (EPC)   我们现在讲解如何编写一个仿真程序——除了 LTE 无线接入网外,还允许仿真 EPC. EP ...

  5. Navicat for mysql 破解

    想用navicat for mysql 连接mysql,发现只能试用30天,感觉挺不爽的,购买的话发现价格一千多,好贵的软件. 所以想要破解一下,网上试了一些方法不行,最后找到了一种方法可以的 破解工 ...

  6. Security » Authorization » 介绍

    Introduction¶ 介绍 77 of 87 people found this helpful Authorization refers to the process that determi ...

  7. Django1.9开发博客(9)- 用户认证

    你应该注意到了一点,当你去新建.修改和删除文章的时候并不需要登录,这样的话任何浏览网站的用户都能随时修改和删除我的文章.这个可不是我想要的! 编辑和删除的认证 我们需要保护post_new, post ...

  8. Clojure学习笔记(二)——函数式编程

    定义 “函数式编程”是一种编程范式(programming paradigm),即如何编写程序的方法论.主要思想是把运算过程尽量写成一系列嵌套的函数调用. 举例来说,现在有这样一个数学表达式: (1 ...

  9. (35)odoo中widget

    widget大全: many2many_tagsone2many_listselectionprogressbarselectionstatusbarhandlemonetarymail_thread ...

  10. 51nod 1445 变色DNA(dij)

    题目链接:51nod 1445 变色DNA 看了相关讨论再去用最短路:val[i][j]之间如果是'Y',说明i可以到达j,并且i到达j的代价是i那行 1到j-1 里面'Y'的数量. 最后,求 0到n ...