如何在xml中验证数据 [英] How to validate data in xml
本文介绍了如何在xml中验证数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我有一个Xml文件,它有两个元素
1.名称
2.性别
性别元素值应始终为男性或女性,如果某人给予其他人,则如何在xml中进行验证文件。你可以指导我或发送片段
Hi All,
I have an Xml file , Its having two elements
1. Name
2. Gender
A gender element value should be always either "Male" or "Female" , how can i retrict if some one give other its should validate at xml file. can you guide me or send snippets
<?xml version="1.0" encoding="utf-8" ?>
<persons>
<person>
<name>Rajesh</name>
<gender>Male</gender>
</person>
<person>
<name>Ramya</name>
<gender>FeMale</gender>
</person>
</persons>
推荐答案
由于F-ES SiteCore提到了一种验证/限制数据的方法是使用< a href =https://msdn.microsoft.com/en-us/library/aa468557.aspx> xsd架构文件 [ ^ ]。
你需要这样的东西: br />
As F-ES SiteCore mentioned one way to validate/restrict data is to use xsd schema file[^].
You need something like this:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" >
<xs:annotation>
<xs:documentation>
-- This schema is ... --
-- Copyright (C) ... --
</xs:documentation>
</xs:annotation>
<xs:element name="persons">
<xs:complexType>
<xs:sequence minOccurs ="1" maxOccurs ="1">
<xs:element name="person" minOccurs ="1" maxOccurs ="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type ="xs:string" />
<xs:element name="gender">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="Male"/>
<xs:enumeration value="Female"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
然后,使用< a href =https://msdn.microsoft.com/en-us/library/system.xml.xmlreadersettings.validationeventhandler%28v=vs.110%29.aspx> ValidationEventHandler [ ^ ]检查xml是否有效。
Then, use ValidationEventHandler[^] to check if xml is valid.
这篇关于如何在xml中验证数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文