gpt4 book ai didi

C# xml 验证

转载 作者:数据小太阳 更新时间:2023-10-29 02:49:54 24 4
gpt4 key购买 nike

我定义了一个xsd:
与 HTML 表格非常相似。行有列,列有元素。

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:attributeGroup name="basics">
<xs:attribute name="title" type="xs:string" use="required"/>
<xs:attribute name="field_id" type="xs:string" use="required"/>
<xs:attribute name="is_mandatory" type="xs:boolean" use="required"/>
</xs:attributeGroup>

<xs:element name="form">
<xs:complexType>
<xs:sequence>
<xs:element name="row">
<xs:complexType>
<xs:sequence>
<xs:element name="col">
<xs:complexType>
<xs:sequence>
<!-- lable -->
<xs:element name="label" type="xs:string"/>

<!-- image -->
<xs:element name="image" >
<xs:complexType>
<xs:attribute name="src" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>

<!-- textbox -->
<xs:element name="textbox">
<xs:complexType>
<xs:attributeGroup ref="basics"/>
<xs:attribute name="hint" type="xs:string" use="optional" default=""/>
</xs:complexType>
</xs:element>

<!-- yesno -->
<xs:element name="yesno">
<xs:complexType>
<xs:attributeGroup ref="basics"/>
</xs:complexType>
</xs:element>

<!-- calendar -->
<xs:element name="calendar">
<xs:complexType>
<xs:attributeGroup ref="basics"/>
</xs:complexType>
</xs:element>

<!-- Select/ multi select -->
<xs:element name="select">
<xs:complexType>
<xs:sequence>
<xs:element name="option"/>
</xs:sequence>
<xs:attributeGroup ref="basics"/>
<xs:attribute name="singleChoice" type="xs:boolean" use="required"/>
</xs:complexType>
</xs:element>

</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="title" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
</xs:schema>

并创建了以下 xml(对我来说是有效的 xml):

<?xml version="1.0" encoding="utf-8" ?>
<form title="title">
<row>
<col>
<textbox title="aaa" field="Name" mandatory="false" hint="aaa"/>
</col>
</row>

<row>
<col>
<textbox title="bbb" field="UID" mandatory="true" hint="bbb"/>
<yesno title="dddddd" field="field-yesno" mandatory="true"/>
</col>
</row>

<row>
<col>
<lable>dddddd</lable>
</col>
</row>

<row>
<col>
<calendar title="cccc" field="StartDate" mandatory="true"/>
</col>
</row>

<row>
<col>
<select title="select" field="ffff" mandatory="true">
<option value="1">option 1</option>
<option value="2" selected="true">option 2</option>
<option value="3">option 3</option>
</select>
</col>
</row>
</form>

当我尝试验证时:

XmlReaderSettings settings = new XmlReaderSettings();
settings.Schemas.Add(null, getAbsolutePath("xml\\form_schema.xsd"));
settings.ValidationType = ValidationType.Schema;
settings.CloseInput = true;
settings.ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings |
XmlSchemaValidationFlags.ProcessIdentityConstraints |
XmlSchemaValidationFlags.ProcessInlineSchema |
XmlSchemaValidationFlags.ProcessSchemaLocation;

// create xml reader
XmlReader reader = XmlReader.Create(new StringReader(xml), settings);
XmlDocument document = new XmlDocument();
document.Load(reader);
document.Validate(new ValidationEventHandler(ValidationHandler));

我遇到以下异常:

The element 'col' has invalid child element 'textbox'. List of possible elements expected: 'label'

我的 xsd 或 C# 代码有什么问题? (xml 是很好的例子)?

最佳答案

问题似乎出在 XSD 上。您已在 col 中指定了一个序列元素,它期待 col看起来像这样:

<col>
<label />
<image />
<textbox />
<yesno />
<calendar />
<select />
</col>

你要么需要输入 minOccurs="0"在每个元素上:

<xs:element name="label" type="xs:string" minOccurs="0" />

或使用 <xs:choice>

关于C# xml 验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23247619/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com