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.* ...
随机推荐
- (转)Could not create the view: An unexpected exception was thrown. 电脑突然断电,myeclipse非正常关闭,出现错误
问题:电脑突然断电,myeclipse非正常关闭,“Package Explorer”非正常显示,出现错误“Could not create the view: An unexpected excep ...
- Moving From Top To Bottom in Detailed Block in Oracle Forms
Suppose you want to scan a tabular grid block (loop through all records in detail block) from top to ...
- VBA中使用JavaScript脚本语言解析JSON数据
JSON:JavaScript 对象表示法(JavaScript Object Notation) 和xml相似,都是文本形式(保存在文本文件中或字符串等形式),比如: jsstr = {" ...
- lua中string.find()函数作用于汉字字符串
lua中有这样一个库函数,string,find(),作用是在一个字符串中找到目标字符串的起始和结束位置(从1开始计数) 如:a,b=string.find("hello world&quo ...
- monkey中的一些问题
一起来看下导致App Crash的那些原因: 1. 空指针异常:错误日志定位java.lang.NullPointerException,详细日志记录如下 2. 安全异常:错误日志定位 ...
- jquery引用方法时传递参数
经常到网上去下载大牛们写的js插件.每次只需将js引用并设置下变量就行了,但一直没搞明白原理(主要是大牛们的代码太简练了-,-). 这次弄清了如何传递.设置多个(很多个)参数. 如 方法为functi ...
- treeview递归加载
实体类: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ...
- 在caffe中使用hdf5的数据
caffe默认使用的数据格式为lmdb文件格式,它提供了把图片转为lmdb文件格式的小程序,但是呢,我的数据为一维的数据,我也要分类啊,那我怎么办?肯定有办法可以转为lmdb文件格式的,我也看了一些源 ...
- STM32学习笔记(四) RCC外设的学习和理解
RCC时钟模块并不好理解,初次接触我也是一头雾水,而且我真正掌握它的时候也比较晚,是我在学习uC/os-II,需要分析时钟时才有了深刻认识.但在学习中我却一定要把放在了前列,因为这是整个嵌入式最重要的 ...
- Running With xpi
d File file = new File("firebug-1.8.1.xpi"); FirefoxProfile firefoxProfile = new FirefoxPr ...