C#与XML Schema的问题
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的问题的更多相关文章
- XML Schema and XMLspy notes
Introduction An xml documents consists of elements, attributes and text. There are two structures in ...
- XSD(XML Schema Definition)用法实例介绍以及C#使用xsd文件验证XML格式
XML Schema 语言也称作 XML Schema 定义(XML Schema Definition,XSD),作用是定义 XML 文档的合法构建模块,类似 DTD,但更加强大. 作用有: ①定义 ...
- XML Schema命名空间解析
URI Web中汇集了各种资源.资源可以是具有标识的任何事物, 如文档. 文件. 菜单项. 计算机. 服务等, 甚至可以包括人. 组织和概念[BernersLee 1998].在Web体系结构中, ...
- 【转】XSD (xml Schema Definition)
来自:http://www.cnblogs.com/newsouls/archive/2011/10/28/2227765.html Xml Schema的用途 1. 定义一个Xml文档中都有什么元 ...
- XML Schema的基本语法(转)
XML Schema的基本语法(转) XSDL(XML Schema定义语言)由元素.属性.命名空间和XML文档种的其他节点构成的. 一.XSD中的元素 XSD文档至少要包含:schema根元素和XM ...
- Xml Schema:C#访问在complextype中插入新元素
最近用c#写Xml Schema代码,找了很久也找不到如何在已有的complextype中插入新的element,最后我充分发挥自己的聪明才智,哈哈,终于从...中找到了灵感. XmlSchemaSe ...
- [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 ...
- 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 ...
- dubbo源码之一——xml schema扩展
dubbo源码版本:2.5.4 dubbo-parent |----dubbo-config |----dubbo-config-api |----com.alibaba.dubbo.config.* ...
随机推荐
- 什么是 HTML?
前言 在 W3C(万维网联盟)官网里,有一套针对于初学者的 HTML 培训教程,为期四周.为了提升自己的翻译水平,同时帮助大家入门,我给大家翻译出来,以供参考. 1. 什么是 HTML HTML 是创 ...
- [问题2014S14] 复旦高等代数II(13级)每周一题(第十四教学周)
[问题2014S14] 设 \(V\) 为酉空间, 证明: 不存在 \(V\) 上的非零线性变换 \(\varphi\), 使得对 \(V\) 中任一向量 \(v\) 均有 \[(\varphi(v ...
- python2 到 python3 转换工具 2to3
windows系统下的使用方法: (1)将python安装包下的Tools/Scripts下面的2to3.py拷贝到需要转换文件目录中. (2)dos切换到需要转换的文件目录下,运行命令2to3.py ...
- jQuery EasyUI教程之datagrid应用(一)
最近一段时间都在做人事系统的项目,主要用到了EasyUI,数据库操作,然后抽点时间整理一下EasyUI的内容. 这里我们就以一个简洁的电话簿软件为基础,具体地说一下datagrid应用吧 datagr ...
- C#程序设计---->计算圆面积windows程序
值得说的就是添加一个回车事件, http://blog.csdn.net/nanwang314/article/details/6176604 private void textBox1_KeyDow ...
- (转)awk实例练习(一)
文章转自 http://www.cnblogs.com/zhuyp1015/archive/2012/07/14/2591822.html 前一篇学习了awk的基本知识,现在来做一些练习加深一下印象. ...
- Servlet 实现上传文件以及同时,写入xml格式文件和上传
package com.isoftstone.eply.servlet; import java.io.BufferedReader; import java.io.BufferedWriter; i ...
- 转!!XML,DTD,XSD,XSL的区别
XML=可扩展标记语言(eXtensible Markup Language).可扩展标记语言XML是一种简单的数据存储语言,使用一系列简单的标记描述数据,而这些标记可用 方便的方式建立,虽然XML占 ...
- swift混编oc碰到的问题
在swift中混编苹果官方的Reachability OC文件. 因为swift工程的target是生成framework而非app,framework中调用oc与app中使用桥接文件还不一样,参考: ...
- Karma Police - Radiohead
音乐赏析似乎是一件没有意义的工作,与电影相比音乐更加抽象,不同的人对同一首歌会有完全不同的解读. 但一首歌一旦成为经典,就有解读它的必要,因为它一定诉出了一个群体的某些情绪. Karma police ...