xml是常用的一種數(shù)據(jù)文件格式,它的定義文件為Xml schema definition(XSD),那么怎么驗(yàn)證一個(gè)xml是否符合它的schema定義呢?
成都創(chuàng)新互聯(lián)是專業(yè)的富源網(wǎng)站建設(shè)公司,富源接單;提供成都網(wǎng)站建設(shè)、成都做網(wǎng)站,網(wǎng)頁(yè)設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行富源網(wǎng)站開發(fā)網(wǎng)頁(yè)制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛(ài)的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來(lái)合作!
本文給出C#的代碼實(shí)現(xiàn)。
存儲(chǔ)在xml.xml文件中
<?xml version="1.0" encoding="utf-8" ?>
<xml>
<age>10</age>
<date>2018-01-01</date>
<regex>111</regex>
<gMonth>---10--</gMonth>
<language>english</language>
<anyURI>./news.html</anyURI>
</xml>
存儲(chǔ)在xsd.xsd文件中
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="xml">
<xs:complexType>
<xs:sequence>
<xs:element name="age">
<xs:simpleType>
<xs:restriction base="xs:decimal">
<xs:enumeration value="10"/>
<xs:enumeration value="20"/>
<xs:enumeration value="30"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="date">
<xs:simpleType>
<xs:restriction base="xs:date">
<xs:enumeration value="2018-01-01Z"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="regex">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[0-9][0-9][0-9]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="gMonth" type="xs:gMonth"/>
<xs:element name="language" type="xs:language"/>
<xs:element name="anyURI" type="xs:anyURI"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
下面代碼在Console Project中實(shí)現(xiàn)
class Program
{
static void Main(string[] args)
{
string xmlFile = "xml.xml";
string xsdFile = "xsd.xsd";
var exceptionMessage = string.Empty;
VerifyXML(xsdFile, xmlFile, ref exceptionMessage);
Console.WriteLine(exceptionMessage);
Console.ReadKey();
}
private static void VerifyXML(string xsdFile, string xmlFile, ref string exceptionMessage)
{
XmlDocument doc = LoadXML(xmlFile);
doc.Schemas = LoadXMLSchmeaFromXSDFile(xsdFile);
string errorMessage = string.Empty;
ValidationEventHandler eventHandler = new ValidationEventHandler(delegate (object sender, ValidationEventArgs e)
{
switch (e.Severity)
{
case XmlSeverityType.Error:
errorMessage += e.Message;
break;
case XmlSeverityType.Warning:
break;
}
});
doc.Validate(eventHandler);
exceptionMessage = errorMessage;
}
private static XmlDocument LoadXML(string xmlFile)
{
XmlDocument doc = new XmlDocument();
doc.Load(xmlFile);
return doc;
}
private static XmlSchemaSet LoadXMLSchmeaFromXSDFile(string path)
{
var schemas = new XmlSchemaSet();
schemas.Add("", XmlReader.Create(path));
return schemas;
}
}
分享文章:驗(yàn)證xml是否符合指定xsd
標(biāo)題URL:http://muchs.cn/article8/gdcjop.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供云服務(wù)器、手機(jī)網(wǎng)站建設(shè)、、動(dòng)態(tài)網(wǎng)站、網(wǎng)站改版、網(wǎng)站設(shè)計(jì)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)