在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
XML Schema 语言也称作 XML Schema 定义(XML Schema Definition,XSD),作用是定义 XML 文档的合法构建模块,类似 DTD,但更加强大。 <?xml version="1.0" encoding="UTF-8"?> <Persons> <Person > <Name>张三</Name> <Age>120</Age> </Person> <Person > <Name>李四</Name> <Age>20</Age> </Person> </Persons> Persons.xsd <?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"> <xs:annotation> <xs:documentation> 本文档定义Persons.xml的格式 </xs:documentation> </xs:annotation> <xs:element name="Persons"> <xs:complexType> <xs:sequence> <xs:element name="Person" maxOccurs="unbounded"> <xs:complexType> <xs:sequence> <xs:element name="Name"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:minLength value="2" /> <xs:maxLength value="4" /> </xs:restriction> </xs:simpleType> </xs:element> <xs:element name="Age"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:pattern value="[1-9][0-9]?|1[01][0-9]|120" /> </xs:restriction> </xs:simpleType> </xs:element> </xs:sequence> <xs:attribute name="id" use="required"> <xs:simpleType> <xs:restriction base="xs:int"></xs:restriction> </xs:simpleType> </xs:attribute> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>
实例2(有命名空间): <?xml version="1.0" encoding="UTF-8"?> <Persons xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.xxx.com/XxxSystem"> <Person > <Name>张三</Name> <Age>120</Age> </Person> <Person > <Name>李四</Name> <Age>20</Age> </Person> </Persons> Persons2.xsd <?xml version="1.0" encoding="UTF-8"?> <xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://www.xxx.com/XxxSystem" > <xs:annotation> <xs:documentation> 本文档定义Persons.xml的格式 </xs:documentation> </xs:annotation> <xs:annotation> <xs:documentation>姓名</xs:documentation> </xs:annotation> <xs:simpleType name="T_Name"> <xs:restriction base="xs:string"> <xs:minLength value="2" /> <xs:maxLength value="4" /> </xs:restriction> </xs:simpleType> <xs:annotation> <xs:documentation>年龄</xs:documentation> </xs:annotation> <xs:simpleType name="T_Age"> <xs:restriction base="xs:string"> <xs:pattern value="[1-9][0-9]?|1[01][0-9]|120" /> </xs:restriction> </xs:simpleType> <xs:annotation> <xs:documentation>属性ID</xs:documentation> </xs:annotation> <xs:simpleType name="T_id"> <xs:restriction base="xs:int"></xs:restriction> </xs:simpleType> <xs:element name="Persons"> <xs:complexType> <xs:sequence> <xs:element name="Person" maxOccurs="unbounded" type="tns:T_Person"/> </xs:sequence> </xs:complexType> </xs:element> <xs:complexType name="T_Person"> <xs:sequence> <xs:element name="Name" type="tns:T_Name" /> <xs:element name="Age" type="tns:T_Age" /> </xs:sequence> <xs:attribute name="id" type="tns:T_id" use="required" /> </xs:complexType> </xs:schema> C# 使用xsd文件验证XML格式 /// <summary> /// 通过xsd验证xml格式是否正确,正确返回空字符串,错误返回提示 /// </summary> /// <param name="xmlFile">xml文件</param> /// <param name="xsdFile">xsd文件</param> /// <param name="namespaceUrl">命名空间,无则默认为null</param> /// <returns></returns> public static string XmlValidationByXsd(string xmlFile, string xsdFile, string namespaceUrl = null) { StringBuilder sb = new StringBuilder(); XmlReaderSettings settings = new XmlReaderSettings(); settings.ValidationType = ValidationType.Schema; settings.Schemas.Add(namespaceUrl, xsdFile); settings.ValidationEventHandler += (x,y)=> { sb.AppendFormat("{0}|", y.Message); }; using (XmlReader reader = XmlReader.Create(xmlFile, settings)) { try { while (reader.Read()); } catch (XmlException ex) { sb.AppendFormat("{0}|", ex.Message); } } return sb.ToString(); }
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论