XML架构----使用Xml架构对象模型
如何使用 Xml 架构对象模型
此示例阐释如何将两个 XML 架构定义语言 (XSD) 架构读取到 SchemaCollection 中,浏览它们表示的架构,并创建表示该架构的虚构 XML 输出。
此示例显示如何使用 XmlSchemaCollection 缓存和检索多个架构,并说明架构对象模型 (SOM) 如何加载和保存有效的 XSD 架构。您也可以使用 SOM 创建使用强类型类的内存中架构。
为了说明如何浏览 SOM,此示例输出已加载到 SchemaCollection 中的两个 XSD 架构的格式化版本。然后,该示例创建一个 XML 流,以举例说明给定架构的用法。
[运行示例] | [查看源代码] |
该示例首先创建一个最终要输出到屏幕上的 XmlTextWriter。该示例创建它的目的是为了利用 XmlTextWriter 的各种方法,以生成格式良好的 XML。这些方法有 WriteStartElement、WriteEndElement 和 WriteAttributeString 等等。然后,该示例创建一个 XmlNameTable,并将该名称表添加到 XmlSchemaCollection。在 XmlSchemaCollection 中,该示例读取两个唯一的 XSD 架构。最后,该示例将 XmlSchemaCollection 中的每个架构写到屏幕上,后面是对一些示例 XML 使用该架构的例子。
...
myXmlTextWriter = new XmlTextWriter(Console.Out);
myXmlTextWriter.Formatting = Formatting.Indented;
myXmlTextWriter.Indentation = 2;
...
XmlNameTable myXmlNameTable = new NameTable();
...
XmlSchemaCollection myXmlSchemaCollection = new XmlSchemaCollection(myXmlNameTable);
...
myXmlSchemaCollection.Add(null, args[0]);
...
myXmlSchemaCollection.Add(null, args[1]);
...
foreach(XmlSchema myTempXmlSchema in myXmlSchemaCollection)
{
myXmlSchema = myTempXmlSchema;
...
// Write out the various schema parts
WriteXSDSchema();
...
// Write out an example of the XML for the schema
WriteExample();
...
}
|
||
| C# | VB | |
下列代码中的函数将 XSD 架构写到屏幕上。从本质上说,这些函数依次通过 XmlSchema 中的每项,确定其类型,然后设置该项输出到屏幕上的格式。
// Write out the XSD
void WriteXSDSchema()
{
myXmlTextWriter.WriteStartElement("schema", XmlSchema.Namespace);
myXmlTextWriter.WriteAttributeString("targetNamespace", myXmlSchema.TargetNamespace);
foreach(XmlSchemaInclude include in myXmlSchema.Includes)
{
myXmlTextWriter.WriteStartElement("include", XmlSchema.Namespace);
myXmlTextWriter.WriteAttributeString("schemaLocation", include.SchemaLocation);
myXmlTextWriter.WriteEndElement();
}
foreach(object item in myXmlSchema.Items)
{
if (item is XmlSchemaAttribute)
WriteXmlSchemaAttribute((XmlSchemaAttribute)item); //attribute
else if (item is XmlSchemaComplexType)
WriteXmlSchemaComplexType((XmlSchemaComplexType)item); //complexType
else if (item is XmlSchemaSimpleType)
WriteXmlSchemaSimpleType((XmlSchemaSimpleType)item); //simpleType
else if (item is XmlSchemaElement)
WriteXmlSchemaElement((XmlSchemaElement)item); //element
else if (item is XmlSchemaAnnotation)
WriteXmlSchemaAnnotation((XmlSchemaAnnotation)item); //annotation
else if (item is XmlSchemaAttributeGroup)
WriteXmlSchemaAttributeGroup((XmlSchemaAttributeGroup)item); //attributeGroup
else if (item is XmlSchemaNotation)
WriteXmlSchemaNotation((XmlSchemaNotation)item); //notation
else if (item is XmlSchemaGroup)
WriteXmlSchemaGroup((XmlSchemaGroup)item); //group
else
Console.WriteLine("Not Implemented.");
}
myXmlTextWriter.WriteEndElement();
}
|
||
| C# | VB | |
下列代码中的函数写出验证为架构的 XML 的样式。从本质上说,这些函数依次通过 XmlSchema 中的每项,在确定其类型后,使用一些示例数据设置该项输出到屏幕上的格式。
// Write out the example of the XSD usage
void WriteExample()
{
foreach(XmlSchemaElement element in myXmlSchema.Elements.Values)
WriteExampleElement(element);
}
// Write some example elements
void WriteExampleElement(XmlSchemaElement element)
{
myXmlTextWriter.WriteStartElement(element.QualifiedName.Name, element.QualifiedName.Namespace);
if (element.ElementType is XmlSchemaComplexType)
{
XmlSchemaComplexType type = (XmlSchemaComplexType)element.ElementType;
if (type.ContentModel != null)
Console.WriteLine("Not Implemented for this ContentModel");
WriteExampleAttributes(type.Attributes);
WriteExampleParticle(type.Particle);
}
else
WriteExampleValue(element.ElementType);
myXmlTextWriter.WriteEndElement();
}
|
||
| C# | VB | |
摘要
- 架构对象模型 (SOM) 加载和保存有效的 XSD 架构。
- SOM 提供了一种创建使用强类型类的内存中架构的简便方法。
- 您可以使用 XmlSchemaCollection 对象缓存和检索多个架构。
- SOM 通过 XmlSchemaCollection 使用 XmlValidatingReader。
浙公网安备 33010602011771号