← Tutti gli articoli
XSD Schema Validation
08 May 2012 ·
C# · Article ·
490 visite
Xml document conforms to a particular pattern: XSD
XSD (short for XML Schema Definition)
You can validate an XML file or document against one or more schemas before reading or processing it. There are a number of reasons to do so: You can get away with less error checking and exception handling. Schema validation picks up errors you might otherwise overlook. Error messages are detailed and informative. To perform validation, plug a schema into an XmlReader, an XmlDocument, or an X-DOM object, and then read or load the XML as you would normally. Schema validation happens automatically as content is read, so the input stream is not read twice.
Supposing you have the following schema validation:
customers.xsd
- <? xml version = "1.0" encoding = "utf-8" ?>
- < xs:schema attributeFormDefault = "unqualified"
- elementFormDefault="qualified"
- xmlns:xs="http://www.w3.org/2001/XMLSchema">
- < xs:element name = "customers" >
- <xs:complexType>
- <xs:sequence>
- <xs:element name="customer" minOccurs="0" maxOccurs="unbounded">
- <xs:complexType>
- <xs:attribute name="name" type="xs:string" use="required" />
- <xs:attribute name="city" type="xs:string" use="required" />
- <xs:attribute name="country">
- <xs:simpleType>
- <xs:restriction base="xs:string">
- <xs:enumeration value="Italy" />
- <xs:enumeration value="USA" />
- </xs:restriction>
- </xs:simpleType>
- </xs:attribute>
- </xs:complexType>
- </xs:element>
- </xs:sequence>
- </xs:complexType>
- </ xs:element >
- </ xs:schema >
You have to validate the following xml file:
customerswithorders.xml
- <? xml version = "1.0" encoding = "utf-8" ?>
- < customers >
- < customer name = "Paolo" city = "Brescia" country = "Italy" >
- <orders>
- <order id="1" idProduct="1" quantity="3" shipped="false" month="January" />
- <order id="2" idProduct="2" quantity="5" shipped="true" month="May" />
- </orders>
- </customer>
- <customer name="Marco" city="Torino" country="Italy">
- <orders>
- <order id="3" idProduct="1" quantity="10" shipped="false" month="July" />
- <order id="4" idProduct="3" quantity="20" shipped="true" month="December" />
- </orders>
- </customer>
- <customer name="Paolo" city="Brescia" country="Italy2">
- <orders>
- <order id="1" idProduct="1" quantity="3" shipped="false" month="January" />
- <order id="2" idProduct="2" quantity="5" shipped="true" month="May" />
- </orders>
- </customer>
- </ customers >
Solution:
- private void button5_Click(object sender, EventArgs e)
- {
- string apppath = Path.GetDirectoryName(Application.ExecutablePath);
- // xml file path
- string filename = apppath + @"\customerswithorders.xml";
- // xsd file path
- string fileSchema = apppath + @"\xsd\customers.xsd";
- // reader settings
- XmlReaderSettings settings = new XmlReaderSettings();
- settings.ValidationType = ValidationType.Schema;
- //settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessInlineSchema;
- settings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;
- settings.Schemas.Add(null, fileSchema);
- settings.ValidationType = ValidationType.Schema;
- settings.ValidationEventHandler += new ValidationEventHandler(XmlValidationError);
- // validation
- using (XmlReader reader = XmlReader.Create(filename, settings))
- {
- while (reader.Read()) ;
- }
- MessageBox.Show("Validation finished",
- "Information",
- MessageBoxButtons.OK,
- MessageBoxIcon.Information);
- }
- private void ValidationCallBack(object sender, ValidationEventArgs e)
- {
- validation = false;
- //Console.WriteLine("Validation Error: {0}", e.Message);
- listBox1.Items.Add(e.Message);
- }
- static void XmlValidationError(object sender, ValidationEventArgs e)
- {
- MessageBox.Show(e.Message,
- e.Severity.ToString(),
- MessageBoxButtons.OK,
- e.Severity == XmlSeverityType.Warning ?
- MessageBoxIcon.Warning
- : MessageBoxIcon.Error);
- }