【问题标题】:XML Schema context type inside a sequence序列中的 XML Schema 上下文类型
【发布时间】:2016-06-21 15:38:51
【问题描述】:

我正在使用 C# 创建一个 XML 模式,但我发现了一个相当奇怪的标签序列,这是我之前从未使用过的。我希望它看起来像这样:

<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"/>

如果你能帮助我,我很感激:)

【问题讨论】:

  • 你有什么问题?
  • 我正在使用 C# 创建此模式,但我不知道如何在上述序列中包含复杂类型
  • 当你说你正在使用 C# 创建一个模式时,你到底是什么意思?你遇到了什么困难?

标签: c# xml visual-studio xsd schema


【解决方案1】:

你最好的朋友是 MSDN...我在下面展示了来自 XmlSchema 文档页面的改编样本;基本上,以与 XSD 结构相同的方式链接 SOM API 调用。

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);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-28
    • 2012-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多