validating XML against my XSD is not working

Posted on 2014-09-02
Last Modified: 2014-09-21
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("", ConfigurationManager.AppSettings["MyXSD"].ToString());
                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)

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


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

Open in new window

attach is the Schema New.xsd.

Question by:RamzyNEbeid
    LVL 21

    Expert Comment

    by:Randy Poole
    can you also attach an xml file you are trying to validate.
    LVL 60

    Expert Comment

    by: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
    LVL 21

    Assisted Solution

    by:Randy Poole
    BTW, you can validate your schema, and xml vs your schema here:
    LVL 60

    Accepted Solution


    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    Introduction In my previous article ( I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
    The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API ( has made its way into the popular lexicon of the English language.  A few years ago, …
    The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now