Asked by:
XML Validation with xsd Error

Question
-
User-1798917180 posted
Hi,
below is my xml which I have it in string variable. I have xsd file and I need to validate my xml with xsd. when I try below c# code I error on line document.LoadXml(xml). Not sure how to trouble shoot. Are my name spaces and schema locations correct in both? since we load xsd using path, do we need xmlns and xsi:schemaLocation attributes. I am new to xml. Why do we need all those?
My XML looks like this:
<?xml version="1.0" encoding="utf-8"?>
<MESSAGE xmlns="http://tempuri.org/SchemaMsg.xsd" xsi:schemaLocation="http://tempuri.org/SchemaMsg.xsd" MessageID="1" ApplicationName="HelloWorld" MessageCreationTime="2018-04-09T17:54:47.85" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<CLIENTS ChildCount="1">
<CLIENT>
<ACTION>Add</ACTION>
<SHORTNAME>xxx</SHORTNAME>
<LONGNAME>DataForms Inc</LONGNAME>
<TYPE>DataMassage</TYPE>
</CLIENT>
</CLIENTS>
</MESSAGE>XSD:
<?xml version="1.0" encoding="utf-8
try
{
string xsdPath = "C:\\TestApps\\WebApplication1\\WebApplication1\\bin\\MessageSchema.xsd";
XmlReader schema = XmlReader.Create(xsdPath);
XmlDocument document = new XmlDocument();
document.LoadXml(xml);
document.Schemas.Add("", schema);
ValidationEventHandler eventHandler = new ValidationEventHandler(ValidationEventHandler);
document.Validate(eventHandler);
string var;
if (validationErrors == "")
var = "success";
else
var = "Failure";
}
catch (Exception)
{
throw;
}"?>
<xsd:schema id="MessageSchema" targetNamespace="http://tempuri.org/SchemaMsg.xsd" elementFormDefault="qualified" xmlns="http://tempuri.org/SchemaMsg.xsd" xmlns:mstns="http://tempuri.org/SchemaMsg.xsd" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="Message" type="MessageType"/>
<xsd:complexType name="MessageType">
<xsd:choice minOccurs="1" maxOccurs="unbounded">
<xsd:sequence>
<xsd:element name="Clients" type="ClientsType" minOccurs="0" maxOccurs="1" />
</xsd:sequence>
</xsd:choice>
<xsd:attribute name="MessageID" type="xsd:string"/>
<xsd:attribute name="ApplicationName" type="xsd:string"/>
<xsd:attribute name="MessageCreationTime" type="xsd:dateTime"/>
</xsd:complexType>
<xsd:complexType name="ClientsType">
<xsd:sequence minOccurs="1" maxOccurs="unbounded">
<xsd:element name="Client" type="ClientType"/>
</xsd:sequence>
<xsd:attribute name="ChildCount" type="xsd:int" />
</xsd:complexType>
<xsd:complexType name="ClientType">
<xsd:sequence>
<xsd:element name="ACTION" type="ActionTypes" />
<xsd:element name="SHORTNAME" type="xsd:string" />
<xsd:element name="LONGNAME" type="xsd:string" />
<xsd:element name="TYPE" type="ClientTypes" />
</xsd:sequence>
</xsd:complexType>
<xsd:simpleType name="ActionTypes">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="Add"/>
<xsd:enumeration value="Update"/>
<xsd:enumeration value="Delete"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="ClientTypes">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="DataMassageA" />
<xsd:enumeration value="DataMassageB" />
</xsd:restriction>
</xsd:simpleType>
</xsd:schema>C# code to validate xml against xsd
protected void Button1_Click(object sender, EventArgs e)
{string xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>
<MESSAGE xmlns=\"http://tempuri.org/SchemaMsg.xsd\" xsi:schemaLocation=\"http://tempuri.org/SchemaMsg.xsd\" MessageID=\"1\" ApplicationName=\"HelloWorld\" MessageCreationTime=\"2018-04-09T17:54:47.85\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">
<CLIENTS ChildCount=\"1\">
<CLIENT>
<ACTION>Add</ACTION>
<SHORTNAME>xxx</SHORTNAME>
<LONGNAME>DataForms Inc</LONGNAME>
<TYPE>DataMassage</TYPE>
</CLIENT>
</CLIENTS>
</MESSAGE>";try
{
string xsdPath = "C:\\WebApplication1\\bin\\MessageSchema.xsd";
XmlReader schema = XmlReader.Create(xsdPath);
XmlDocument document = new XmlDocument();
document.LoadXml(xml);
document.Schemas.Add("", schema);
ValidationEventHandler eventHandler = new ValidationEventHandler(ValidationEventHandler);
document.Validate(eventHandler);
string var;
if (validationErrors == "")
var = "success";
else
var = "Failure";
}
catch (Exception)
{
throw;
}}
private void ValidationEventHandler(object sender, ValidationEventArgs e)
{
validationErrors += e.Message + "<br />";
}Thanks
Thursday, April 12, 2018 3:44 AM
All replies
-
User-1838255255 posted
Hi Spunny,
According to your description and code, as far as i know, you want to validate the xml with XSD, but meet error in this line document.LoadXml(xml), could you share us your exception message? Here are some tutorials about the how to achieve it, please check:
How To Validate XML Using XSD In C#:
https://www.c-sharpcorner.com/article/how-to-validate-xml-using-xsd-in-c-sharp/
How to: Validate Using XSD (LINQ to XML) (C#):
Best Regards,
Eric Du
Friday, April 13, 2018 8:51 AM