Link to home
Start Free TrialLog in
Avatar of RamzyNEbeid
RamzyNEbeidFlag for Egypt

asked on

validating XML against my XSD is not working

Dear all

i am wroking with VS2010, C#, Web Services

i have an XSD file and XML file.
i want to validate the XML against the XSD.

i tried a lot of trials and it is not working.

here is my code:
internal static bool ValidXmlDoc(string xml)
        {
            if (xml == "")
            {
                return false;
            }

            bool isValidXml = true;
            XmlValidatingReader vr;
            XmlTextReader tr;
            XmlSchemaCollection schemaCol = new XmlSchemaCollection();

            schemaCol.Add("http://www.f-eg.com/ebpp/IFXMessages/", ConfigurationManager.AppSettings["MyXSD"].ToString());
         
            try
            {
                StringReader srXML = new StringReader(xml);
                // Read the xml.
                tr = new XmlTextReader(srXML);

                // Create the validator.
                vr = new XmlValidatingReader(tr);

                // Set the validation type.
                vr.ValidationType = ValidationType.Schema;

                // Add the schema.
                if (schemaCol != null)
                {
                    vr.Schemas.Add(schemaCol);
                }

                // Read the xml schema.
                while (vr.Read())
                {
                    /*
                     * There is no code here
                     * It automatically checks the given request with the schema
                     */
                }

                vr.Close();

                return isValidXml;
            }
            catch (Exception)
            {
                return false;
            }
            finally
            {
                // Clean up...
                vr = null;
                tr = null;
            }
        }

Open in new window


attach is the Schema New.xsd.

thanks,
Avatar of Randy Poole
Randy Poole
Flag of United States of America image

can you also attach an xml file you are trying to validate.
Avatar of Gertone (Geert Bormans)
The schema is not complete
You are only declaring two complexTypes, you need to declare at least one global element that you can use as the root of your document.
Inside the complex type definitions you are referenceing other types that are not declared in a global fashin in your schema.
This schema is not complete, you likely missed some imports of other schema components.
As Randy says, a copy of the source XML would help.
But at least you know the schema is not complete
SOLUTION
Avatar of Randy Poole
Randy Poole
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial