locked
XML Validation with xsd Error RRS feed

  • 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