很早以前看过一句话:“XML就象空气”,在企业应用开发中XML是一个重要的数据交换标准。而XSD则可以用来校验XML的数据格式是否正确。
一个典型的XSD文件如下:
1 <?xml version="1.0" encoding="UTF-8"?>
2
3
4
5
6 运单
7
8
9
10
11
12
13
14
15 运单前缀只有输入3位数字
16
17
18
19
20
21
22
23
24
25 运单号只能输入8位数字
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41 物流参与者至少要有2个
42
43
44
45
46
47 物流参考者类型,只能是A/S/C其中之一
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
View Code
看到这一大段xml,第一反应通常是头晕,幸好这些内容不用纯手动编写,已经有很多现成的工具,比如XmlSpy可以方便的以GUI方式,通过轻点鼠标,拖拖拉拉就能完成XSD的开发。
这是XmlSpy中XSD的可视化设计界面,还能切换不同的视图,比如下面这样:
对于首次接触XmlSpy的朋友,强烈推荐看下安装目录下的Tutorial.pdf,这是一个不错的入门教程,30分钟以前绝对可以快速浏览一遍。
C#中可以方便的使用XSD来验证xml文件的正确性,示例代码如下:
1 usingSystem;2 usingSystem.Xml;3
4 namespaceXsdValidate5 {6 classProgram7 {8 static void Main(string[] args)9 {10 string xmlFile = @"C:\Users\jimmy.yang\Desktop\XMLSPY\TEST\sample.xml";11 string xsdFile = @"C:\Users\jimmy.yang\Desktop\XMLSPY\TEST\sample.xsd";12
13 var xsdValidateResult =ValidateXml(xmlFile, xsdFile);14
15 if(xsdValidateResult.Item1)16 {17 Console.WriteLine("校验通过!");18 }19 else
20 {21 Console.WriteLine("校验失败,原因:\n" +xsdValidateResult.Item2);22 }23 Console.Read();24
25 }26
27 ///
28 ///使用xsd验证xml是否正确29 ///
30 /// xml文件路径
31 /// xsd文件路径
32 ///
33 static Tuple ValidateXml(string xmlFilePath, stringxsdFilePath)34 {35 Tuple result = new Tuple(true, "");36 XmlReaderSettings st = newXmlReaderSettings();37 st.ValidationType =ValidationType.Schema;38 st.Schemas.Add(null, xsdFilePath);39
40 //设置验证xml出错时的事件。
41 st.ValidationEventHandler += (obj, e) =>
42 {43 result = new Tuple(false, e.Message);44 };45
46 XmlReader xr =XmlReader.Create(xmlFilePath, st);47 while(xr.Read())48 {49 if(xr.IsStartElement())50 {51 xr.Read();52 }53 }54 xr.Close();55 returnresult;56 }57 }58 }
View Code
注意:如果节点采用pattern,即正则表达式验证,比如
XMLSpy中,该节点必须填写"^12345678$"才能验证通过,而如果用.NET/JAVA写代码验证的话,^、$能自动识别为"匹配字符开头/结尾"
XSD还能方便的生成c#类,有二种方法:
1、XMLSpy里先打开一个XSD文件,然后 DTD/Schema->Generate Program Code,接下来按提示操作即可
注:XMLSpy生成的c#类太过于复杂,我个人觉得有点啰嗦
2、直接使用vs.net自带的xsd命令
vs.net命令行下,输入
xsd "xsd文件所在的路径" /classes /out:"cs文件的输出目录"
即可生成对应的cs类 ,文中最开头的xsd生成的cs类代码如下:
1 //------------------------------------------------------------------------------2 //3 //This code was generated by a tool.4 //Runtime Version:4.0.30319.183315 //
6 //Changes to this file may cause incorrect behavior and will be lost if7 //the code is regenerated.8 //9 //------------------------------------------------------------------------------
10
11 usingSystem.Xml.Serialization;12
13 //
14 //This source code was auto-generated by xsd, Version=4.0.30319.1.15 //
16
17
18 ///
19 [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]20 [System.SerializableAttribute()]21 [System.Diagnostics.DebuggerStepThroughAttribute()]22 [System.ComponentModel.DesignerCategoryAttribute("code")]23 [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]24 [System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]25 public partial classAWB {26
27 privateAWBAWBINFO aWBINFOField;28
29 privateAWBPARTICIPANT[] pARTINFOField;30
31 ///
32 [System.Xml.Serialization.XmlElementAttribute("AWB-INFO")]33 publicAWBAWBINFO AWBINFO {34 get{35 return this.aWBINFOField;36 }37 set{38 this.aWBINFOField =value;39 }40 }41
42 ///
43 [System.Xml.Serialization.XmlArrayAttribute("PART-INFO")]44 [System.Xml.Serialization.XmlArrayItemAttribute("PARTICIPANT", IsNullable=false)]45 publicAWBPARTICIPANT[] PARTINFO {46 get{47 return this.pARTINFOField;48 }49 set{50 this.pARTINFOField =value;51 }52 }53 }54
55 ///
56 [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]57 [System.SerializableAttribute()]58 [System.Diagnostics.DebuggerStepThroughAttribute()]59 [System.ComponentModel.DesignerCategoryAttribute("code")]60 [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]61 public partial classAWBAWBINFO {62
63 private stringaWBPREField;64
65 private stringaWBNOField;66
67 ///
68 [System.Xml.Serialization.XmlElementAttribute(DataType="positiveInteger")]69 public stringAWBPRE {70 get{71 return this.aWBPREField;72 }73 set{74 this.aWBPREField =value;75 }76 }77
78 ///
79 [System.Xml.Serialization.XmlElementAttribute(DataType="positiveInteger")]80 public stringAWBNO {81 get{82 return this.aWBNOField;83 }84 set{85 this.aWBNOField =value;86 }87 }88 }89
90 ///
91 [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]92 [System.SerializableAttribute()]93 [System.Diagnostics.DebuggerStepThroughAttribute()]94 [System.ComponentModel.DesignerCategoryAttribute("code")]95 public partial classAddressType {96
97 private stringnameField;98
99 private stringstreetField;100
101 private stringcityField;102
103 ///
104 public stringName {105 get{106 return this.nameField;107 }108 set{109 this.nameField =value;110 }111 }112
113 ///
114 public stringStreet {115 get{116 return this.streetField;117 }118 set{119 this.streetField =value;120 }121 }122
123 ///
124 public stringCity {125 get{126 return this.cityField;127 }128 set{129 this.cityField =value;130 }131 }132 }133
134 ///
135 [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]136 [System.SerializableAttribute()]137 [System.Diagnostics.DebuggerStepThroughAttribute()]138 [System.ComponentModel.DesignerCategoryAttribute("code")]139 [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]140 public partial classAWBPARTICIPANT {141
142 privateAWBPARTICIPANTTYPE tYPEField;143
144 privateAddressType aDDRESSField;145
146 ///
147 publicAWBPARTICIPANTTYPE TYPE {148 get{149 return this.tYPEField;150 }151 set{152 this.tYPEField =value;153 }154 }155
156 ///
157 publicAddressType ADDRESS {158 get{159 return this.aDDRESSField;160 }161 set{162 this.aDDRESSField =value;163 }164 }165 }166
167 ///
168 [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]169 [System.SerializableAttribute()]170 [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]171 public enumAWBPARTICIPANTTYPE {172
173 ///
174 C,175
176 ///
177 S,178
179 ///
180 A,181 }
View Code
xsd命令还能直接根据xml生成xsd文件,使用方法如下:
xsd c:\sampe.xml /out:c:\
这样会根据sample.xml在c:\生成sample.xsd文件