Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 231
  • Last Modified:

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,
0
RamzyNEbeid
Asked:
RamzyNEbeid
  • 2
  • 2
2 Solutions
 
Randy PooleCommented:
can you also attach an xml file you are trying to validate.
0
 
Geert BormansCommented:
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
0
 
Randy PooleCommented:
BTW, you can validate your schema, and xml vs your schema here:
http://www.utilities-online.info/xsdvalidation/#.VAWzjz_4WCc
0
 
Geert BormansCommented:
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now