XML (part 4, advanced), XSD verification

Data integrity is a big issue in XML. You can generate XML Schemas to define your data structure and automate data integrity checks. As mentioned before, I am not going to explain the XSD structure itself. My purpose here is to explain the C# part.
Follow the link for a brilliant XSD introduction.

We are still using the embedded “Walmart.xml” file from the previous posts (1, 2, 3).

The easiest way to create an XML Schema is to use Visual Studio 2013. Open the XML file “Walmart.xml”. Then select “Create Schema” in the Window-Menu “XML” (between TEAM and TOOLS). The resulting XSD file looks like this:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="Walmart">
    <xs:complexType>
      <xs:sequence>
        <xs:choice maxOccurs="unbounded">
          <xs:element maxOccurs="unbounded" name="food">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="name" type="xs:string" />
                <xs:element name="price" type="xs:decimal" />
                <xs:element name="description" type="xs:string" />
              </xs:sequence>
            </xs:complexType>
          </xs:element>
          <xs:element name="electronic">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="name" type="xs:string" />
                <xs:element name="price" type="xs:unsignedByte" />
                <xs:element name="description" type="xs:string" />
                <xs:element name="somethingElse" type="xs:string" />
              </xs:sequence>
            </xs:complexType>
          </xs:element>
        </xs:choice>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

You can play with this file and modify it later on. Btw. one line should be amended from

<xs:element name="price" type="xs:unsignedByte" />

to

<xs:element name="price" type="xs:decimal" />

But for now let’s stick to the automatically generated XSD file as it is. Add the file to your Solution Explorer right next to “Walmart.xml” and set the property “Build Action” of “Walmart.xsd” to “Embedded Resource”.

private static void ValidationEvents(object sender, ValidationEventArgs e) {
    switch (e.Severity) {
        case XmlSeverityType.Error:
            Console.WriteLine("Error: " + e.Message);
            break;
        case XmlSeverityType.Warning:
            Console.WriteLine("Warning: " + e.Message);
            break;
    }
} //

public static void ValidateXML() {
    Assembly lAssembly = Assembly.GetExecutingAssembly();
    using (Stream lStreamXml = lAssembly.GetManifestResourceStream("DemoApp.XmlFiles.Walmart.xml")) {
        using (Stream lStreamXsd = lAssembly.GetManifestResourceStream("DemoApp.XmlFiles.Walmart.xsd")) {
            XmlDocument lXmlDocument = new XmlDocument();
            XmlSchema x = XmlSchema.Read(lStreamXsd, new ValidationEventHandler(ValidationEvents));
            lXmlDocument.Schemas.Add(x);
            lXmlDocument.Load(lStreamXml);

            lXmlDocument.Validate(ValidationEvents);
        }
    }

    Console.WriteLine("press enter to exit program");
    Console.ReadLine();
} //

Run ValidateXML(). There should be no error message. Now change the XSD or XML file and see what error messages you get.

Advertisements

About Bastian M.K. Ohta

Happiness only real when shared.

Posted on January 22, 2014, in Advanced, C#, XML and tagged , , , , , , , , . Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

%d bloggers like this: