Shehzad

Shehzad

  • NA
  • 241
  • 19.9k

XSD Validation error

Mar 10 2015 12:37 PM
Hi ,
 I am trying to validate XML thru XSD for the first time.
tried all possible ways but unable to validate.
Could anyone let what I am missing.
XML File
--------
<?xml version="1.0" encoding="utf-8"?>
<WMS>
  <Detail>
  <LineNo>1</LineNo>
  <OrderedQty>600.000</OrderedQty>
  <ExpiryDate>12/2/2015 6:35:09 AM</ExpiryDate>
  <FIFODate>6/2/2013 6:35:09 AM</FIFODate>
  </Detail>
</WMS> 
-------- 
XSD File
----------
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="XMLStruc"
  targetNamespace="http://tempuri.org/XMLStruc.xsd"
  elementFormDefault="qualified"
  xmlns="http://tempuri.org/XMLStruc.xsd"
  xmlns:mstns="http://tempuri.org/XMLStruc.xsd"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
  <xs:element name="WMS">
  <xs:complexType >
  <xs:sequence>
  <xs:element name ="Detail"  minOccurs ="1" maxOccurs ="unbounded">
  <xs:complexType >
  <xs:sequence>
  <xs:element name ="LineNo" type ="xs:string"></xs:element>
  <xs:element name ="OrderedQty" type ="xs:string"></xs:element>
  <xs:element name ="ExpiryDate" type ="xs:dateTime"></xs:element>
  <xs:element name ="FIFODate" type ="xs:dateTime"></xs:element>
  </xs:sequence> 
  </xs:complexType >
  </xs:element>
  </xs:sequence>
  </xs:complexType>
  </xs:element> 
</xs:schema>
---------- 
C# Validation code
-------
  private void button2_Click(object sender, EventArgs e)
  {
  XmlSchemaSet schemas = new XmlSchemaSet();
  schemas.Add("http://tempuri.org/XMLStruc.xsd", @"c:\shz\xmlcreate\XMLCREATE\XMLCREATE\XMLStruc.xsd");
  XmlReaderSettings settings = new XmlReaderSettings();
  settings.ValidationType = ValidationType.Schema;
  //settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessInlineSchema;
  //settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessSchemaLocation;
  settings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;
  settings.Schemas = schemas;
  settings.ValidationEventHandler += ValidationError;
  // create the XmlReader object
  XmlReader reader =
  XmlReader.Create("Sample.xml", settings);
  // parse the file
  while (reader.Read()) ; // empty body
  if (valid) // check validation result
  {
  textBox1.Text = "Document is valid";
  } // end if
  valid = true; // reset variable
  reader.Close(); // close reader stream
  }
  // event handler for validation error
  private void ValidationError(object sender,ValidationEventArgs arguments)
  {
  textBox1.Text = arguments.Message;
  valid = false; // validation failed
  }
-------
------- 
Regards
Shehzad