1

In my schema (xsd), I have an element with minOccurs=1, which I am treating as an required field and I want my application to make sure that field is provided by the clients in the xml. But how do I check for it explicitly. For example here is my element:

XSD:

xs:element minOccurs="1" maxOccurs="1" name="COL_FIRST_NAME" type="xs:string"/>

Xml:

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<Employee>
  <FIRSTNAME />
</Employee>
</xs:schema>

If I need to validate this in my C# code is the hard check the only way? Like

if (FirstName=="")
{
   Console.Write("The firstName cannot be empty");
}

Please note, I cannot use XmlReaderSettings.ValidationType. As my xml output is mapped with another Schema(xsd) for that I am using schema validation and it works well. But while writing the data to DB, I have another schema (the one I provided). This schema is to ensure that these specific elements are entered into DB without fail.

marak
  • 260
  • 3
  • 19
  • This answer http://stackoverflow.com/a/11801566/69527 shows how to specify the schema to use for validation. – CoderDennis Aug 05 '15 at 18:17
  • Thanks for your reply. But as I mentioned I cannot use the inbuilt schema validation in here. As the XSD element names and xml elements dont match. – marak Aug 05 '15 at 19:16
  • Your question shows that the XSD and xml both have `FIRSTNAME`. Which part doesn't match? What are you using to write to the DB? Do you want to extract rules from the XSD and turn them into DB validation? – CoderDennis Aug 05 '15 at 20:20
  • Sorry for the confusion. Made them different in the question. Yes thats correct, I am trying to extract rules from XSD and map them to the column names in the DB. The XSD I have is a schema of the tables in the db – marak Aug 05 '15 at 20:26
  • OK. That leads to another question. How are you planning to map `COL_FIRST_NAME` to `FIRSTNAME`? It doesn't seem like using the XSD directly to validate your DB writes is a good idea. – CoderDennis Aug 05 '15 at 20:32
  • Currently, I auto generated a class using Xsd.exe and then I am mapping the properties with the xml elements . The object properties are then passed as sql parameters and writing them to db. – marak Aug 05 '15 at 20:37

0 Answers0