Is Xml Valid : Schema « XML « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# / C Sharp » XML » SchemaScreenshots 
Is Xml Valid
  

using System;
using System.Collections.Generic;
using System.Text;

using System.IO;
using System.Xml;
using System.Xml.Schema;
using System.Reflection;

namespace Winsmarts.PI.Common
{
    public static class Utilities
    {
        public static bool IsXmlValid(string xmlFile, string xmlSchema)
        {
            TextReader schemaStream =
                new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream(xmlSchema));
            XmlSchemaSet schemaSet = new XmlSchemaSet() ;
            schemaSet.Add("",XmlReader.Create(schemaStream));

            Stream docStream = new FileStream(xmlFile, FileMode.Open, FileAccess.Read);
            XmlReaderSettings settings = new XmlReaderSettings() ;
            settings.Schemas.Add(schemaSet;
            settings.ValidationType = ValidationType.Schema;
            XmlReader xRead = XmlReader.Create(docStream, settings)  ;

            bool toReturn = false;

            try
            {
                while (xRead.Read())
                {
                    // do nothing :-/
                }
                toReturn = true;
            }
            catch (XmlSchemaValidationException xExcp)
            {
                Trace.WriteLine("Input document " + xmlFile + " does not match the schema"true);
                Trace.WriteLine(xExcp.ToString()true);
            }

            return toReturn;
        }
    }
}

   
    
  
Related examples in the same category
1.Set XmlReaderSettings
2.Choose ValidationType
3.Validate an XML Document Against a Schema
4.Validate Schema
5.Use XML schema to validate XML documents
6.Use XmlReaderSettings to validate the Xml document
7.Strip Non Valid XML Characters.
8.Is Well Formed Xml
9.XmlSchema is an in-memory representation of an XML Schema
10.Xml Validation Helper
11.Get Intrinsic Simple Types Names from System.Xml.Schema.DatatypeImplementation
12.Reads a XML schema file and returns the information found in that.
13.XML reading functionality
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.