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. 单元测试:查找list[]中的最大值

     原始代码如下: int Largest(int list[], int length) { int i,max; for(i = 0; i < (length – 1); i ++ ) { i ...

  2. viewport和media query

    viewport: 你可以定义viewport的宽度.如果你不使用width=device-width,在移动端上你的页面延伸会超过视窗布局的宽度(width=980px),如果你使用了width=d ...

  3. codeforces 446A DZY Loves Sequences

    vjudge 上题目链接:codeforces 446A 大意是说最多可以修改数列中的一个数,求最长严格递增的连续子序列长度. 其实就是个 dp 的思想,想好思路后交上去没想到一直 wa 在第二个测试 ...

  4. 【转载】CSS 伪类-:before和:after

    :before和:after的作用就是在指定的元素内容(而不是元素本身)之前或者之后插入一个包含content属性指定内容的行内元素,最基本的用法如下: #example:before { conte ...

  5. Head First 设计模式 --5 单例模式

    单例模式:确保一个类只有一个实例,并提供一个全局访问点.用到的设计原则:1.封装变化2.组合优于集成3.针对接口变成而不是针对实现4.为交互对象之间的松耦合设计而努力5.类应该对扩展开放,对修改关闭6 ...

  6. 5 分钟上手 ECharts

    获取 ECharts 你可以通过以下几种方式获取 ECharts. 从官网下载界面选择你需要的版本下载,根据开发者功能和体积上的需求,我们提供了不同打包的下载,如果你在体积上没有要求,可以直接下载完整 ...

  7. Unity NGUI 2D场景添加按钮

    比如说先添加一个sprite 在sprite加上NGUI的 UI Button 然后重点来了  加上Box Collider 2D(重点:2D 千万不要加 Box Collider) 将Box Col ...

  8. JAVA中怎么处理高并发的情况

    一.背景综述 并发就是可以使用多个线程或进程,同时处理(就是并发)不同的操作. 高并发的时候就是有很多用户在访问,导致系统数据不正确.糗事数据的现象.对于一些大型网站,比如门户网站,在面对大量用户访问 ...

  9. 第八章 企业项目开发--分布式缓存memcached

    注意:本节代码基于<第七章 企业项目开发--本地缓存guava cache> 1.本地缓存的问题 本地缓存速度一开始高于分布式缓存,但是随着其缓存数量的增加,所占内存越来越大,系统运行内存 ...

  10. centos7.0 64位系统安装 nginx

    1 下载nginx 从nginx官网 http://nginx.org/ 下载新的稳定版本nginx 并上传到linux服务器 2 安装nginx 所需要的扩展 yum -y install pcre ...