[英]XML Schema context type inside a sequence
我正在使用C#创建XML Schema,但是我发现了一个奇怪的标签序列,之前从未使用过。 我希望它看起来像这样:
<xsd:complexType name="Sensor_Info">
<xsd:sequence>
<xsd:element name="Sensor" minOccurs="1" maxOccurs="unbounded">
<xsd:complexType>
<xsd:attribute name="id" type="xsd:string"/>
<xsd:attribute name="name" type="xsd:string"/>
<xsd:attribute name="type" type="xsd:string"/>
<xsd:attribute name="location_id" type="xsd:string"/>
<xsd:attribute name="unit" type="xsd:string"/>
<xsd:attribute name="min_value" type="xsd:int"/>
<xsd:attribute name="max_value" type="xsd:int"/>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
<xsd:attribute name="count" type="xsd:int"/>
如果您能帮助我,我表示感谢:)
您最好的朋友是MSDN。我在下面显示的是XmlSchema文档页面的改编示例。 基本上,链接SOM API调用的方式与XSD结构相同。
using System;
using System.Xml;
using System.Xml.Schema;
class XMLSchemaExamples
{
public static void Main()
{
XmlSchema schema = new XmlSchema();
var ct = new XmlSchemaComplexType {Name = "Sensor_Info"};
schema.Items.Add(ct);
var at = new XmlSchemaAttribute
{
Name = "count",
SchemaTypeName = new XmlQualifiedName("int", XmlSchema.Namespace)
};
ct.Attributes.Add(at);
var seq = new XmlSchemaSequence();
ct.Particle = seq;
var sensor = new XmlSchemaElement {Name = "Sensor", MaxOccursString = "unbounded"};
seq.Items.Add(sensor);
var sensorType = new XmlSchemaComplexType();
sensor.SchemaType = sensorType;
at = new XmlSchemaAttribute
{
Name = "id",
SchemaTypeName = new XmlQualifiedName("string", XmlSchema.Namespace)
};
sensorType.Attributes.Add(at);
// TODO add more attributes here
XmlSchemaSet schemaSet = new XmlSchemaSet();
schemaSet.ValidationEventHandler += ValidationCallbackOne;
schemaSet.Add(schema);
schemaSet.Compile();
var nsmgr = new XmlNamespaceManager(new NameTable());
nsmgr.AddNamespace("xsd", "http://www.w3.org/2001/XMLSchema");
schema.Write(Console.Out, nsmgr);
}
public static void ValidationCallbackOne(object sender, ValidationEventArgs args)
{
Console.WriteLine(args.Message);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.