Ejemplo n.º 1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public XsdFile Load(string path)
        {
            // load the new xsd
            if (File.Exists(path) == false)
            {
                //TODO : create a specific extesion
                throw new Exception("XSD File doest not exist");
            }

            XsdFile xsdFileInfo = new XsdFile()
            {
                Name = Path.GetFileName(path),
                Path = path
            };

            this.xsds.Add(path, xsdFileInfo);

            return(xsdFileInfo);
        }
Ejemplo n.º 2
0
    static void main()
    {
        XsdFile file = new XsdFile(@"c:\temp\test.xsd");

        Console.WriteLine(file.Query("Setup_Type"));
    }
        /// <summary>
        ///
        /// </summary>
        /// <param name="xmlFile"></param>
        /// <returns></returns>
        private bool ValidateAgainstXsd(XmlFile xmlFile)
        {
            if (xmlFile.HasDeclaredXsd == false)
            {
                // no xsd validation
                return(true);
            }

            if (File.Exists(xmlFile.XsdFilePath) == false)
            {
                // warning no xsd to validate against
                this.Errors.Add(new XmlError()
                {
                    FilePath     = xmlFile.FilePath,
                    Column       = 0,
                    Line         = 0,
                    ErrorType    = XmlErrorType.SEMANTIC,
                    ErrorCode    = 0,
                    Message      = DiagnosticMessages.NoXSDSchemaDefined(),
                    ErrorGravity = XmlErrorGravity.WARNING
                });

                return(false);
            }

            try
            {
                XsdFile xsdFile = xsdCache.Get(xmlFile.XsdFilePath);

                XDocument xDocument = XDocument.Parse(xmlFile.Content, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo | LoadOptions.SetBaseUri);

                // Create the XmlSchemaSet class.
                XmlSchemaSet schemaSet = new XmlSchemaSet();

                // Add the schema to the collection.
                schemaSet.Add(null, xsdFile.Path);

                // validate the document
                xDocument.Validate(schemaSet, null);
            }
            catch (XmlSchemaValidationException ex)
            {
                this.Errors.Add(new XmlError()
                {
                    FilePath     = xmlFile.FilePath,
                    Line         = ex.LineNumber - 1,
                    Column       = ex.LinePosition - 1,
                    Message      = DiagnosticMessages.XmlReadingError(ex.Message),
                    ErrorType    = XmlErrorType.SEMANTIC,
                    ErrorCode    = 1,
                    ErrorGravity = XmlErrorGravity.ERROR
                });

                return(false);
            }
            catch (XmlException ex)
            {
                this.Errors.Add(new XmlError()
                {
                    FilePath     = xmlFile.FilePath,
                    Line         = ex.LineNumber - 1,
                    Column       = ex.LinePosition - 1,
                    Message      = DiagnosticMessages.XmlReadingError(ex.Message),
                    ErrorType    = XmlErrorType.SYNTAX,
                    ErrorCode    = 1,
                    ErrorGravity = XmlErrorGravity.ERROR
                });

                return(false);
            }
            catch (Exception ex)
            {
                this.Errors.Add(new XmlError()
                {
                    FilePath     = xmlFile.FilePath,
                    Line         = 0,
                    Column       = 0,
                    Message      = DiagnosticMessages.XmlReadingError(ex.Message),
                    ErrorType    = XmlErrorType.SYNTAX,
                    ErrorCode    = 2,
                    ErrorGravity = XmlErrorGravity.ERROR
                });
                return(false);
            }

            return(true);
        }