Hi,
below the sample code to play with
XSD:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:element name="User">
<xs:complexType>
<xs:sequence>
<xs:element ref="Name" />
<xs:element ref="BirthTime" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name = "Name">
<xs:simpleType>
<xs:restriction base = "xs:string">
<xs:pattern value="[a-zA-Z0-9]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="BirthTime">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="(0[0-9]|[1][0-9]|2[1-3]):([0-5][0-9])"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:schema>
xml:
<?xml version="1.0" encoding="utf-8"?>
<User>
<Name>str1234</Name>
<BirthTime>20:11</BirthTime>
</User>
c# code to test:
private void button1_Click(object sender, EventArgs e)
{
XmlSchemaSet schemas = new XmlSchemaSet();
schemas.Add("",@"D:\XmlTesting\user.xsd");
XDocument doc = XDocument.Load(@"D:\XmlTesting\user.xml");
string msg = "";
doc.Validate(schemas, (o, es) =>
{
msg += es.Message + Environment.NewLine;
});
MessageBox.Show(msg == "" ? "Document is valid" : "Document invalid: " + msg);
}
The requirement:
Name should not be empty and should not contain special characters.
BirthTime should be in HH:MM format with 24 hour format and it should not be empty as well.
When i try to run the validation code as mentioend about i am getting the error as
The 'Name' element is invalid - The value 'str1234' is invalid according to its datatype 'String' - The Pattern constraint failed.
The 'BirthTime' element is invalid - The value '20:11' is invalid according to its datatype 'String' - The Pattern constraint failed
any help please