Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I would like to validate my input object using the xsd file I defined. Would you please let me know how to do this?

below is my c# object

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BaseModel
{
    public class Test
    {
        public string TestID { get; set; }

        public string SocialSecurityNo { get; set; }

        public string OrgSelection { get; set; }

        public string FirstName { get; set; }

        public string MiddleName { get; set; }

        public string LastName { get; set; }
    }
}


below is the XML I get after serializing my input object

XML
<?xml version="1.0" encoding="utf-8" ?>
<Personnel>
  <EnterpriseID>1</EnterpriseID>
  <SocialSecurityNo>12345678</SocialSecurityNo>
  <OrgSelection/>
  <MiddleName/>
  <LastName>Tharigopula</LastName>
</Personnel>


Below is the XSD file I generated using the input object
XML
<xs:schema elementformdefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="Personnel" nillable="true" type="Personnel" />
  <xs:complextype name="Test">
    <xs:sequence>
      <xs:element minoccurs="1" maxoccurs="1" name="TestID" type="xs:string" />
      <xs:element minoccurs="1" maxoccurs="1" name="SocialSecurityNo" type="xs:string" />
      <xs:element minoccurs="0" maxoccurs="1" name="OrgSelection" type="xs:string" />
      <xs:element minoccurs="1" maxoccurs="1" name="FirstName" type="xs:string" />
      <xs:element minoccurs="0" maxoccurs="1" name="MiddleName" type="xs:string" />
      <xs:element minoccurs="1" maxoccurs="1" name="LastName" type="xs:string" />
    </xs:sequence>
  </xs:complextype>
</xs:schema>


As you can see in the XSD, I have the TestId, SocialSecurityNo, FirstName, and LastName or the mandatory fields and OrgSelection and MiddleName or optional fields..

so if I were validate my input object without passing the FirstName and LastName, I should see the validation errors..

Can someone please help?
Posted
Updated 29-Dec-15 6:11am
v4

1 solution

Hi Ravindranath.net,

You can validate a XML against a XSD using the Validate extension method.

Here is the code showing how to do it.

C#
XmlSchemaSet schemaSet = new XmlSchemaSet();
DirectoryInfo dir = new DirectoryInfo("<path where="" you="" have="" your="" xsd="" files="">");

 foreach (FileInfo fileInfo in dir.GetFiles("*.xsd"))
 {
     schemaSet.Add(null, fileInfo.FullName);
 }
 XDocument docToValidate = XDocument.Load("<path of="" xml="" you="" want="" to="" validate="">");
 string validationMessage = string.Empty;
 docToValidate.Validate(schemaSet, (o, e) =>
 {
     validationMessage += e.Message + Environment.NewLine;
 });
 return validationMessage;</path></path>


This code may need some customization to meet your requirements. Hope this helps.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900