验证了XSD XML Schema的不使用C#更改XML

问题描述:

我有没有在一个XML架构的XML文件,我需要验证针对XSD架构的XML。我已经看到你在哪里注入XSD到XML,然后验证XML的例子很多。我并不想改变的XML,是否有可能验证XML,对不改变架构中的XML?

I have an XML file without a Schema in the XML, and I need to validate the XML against a XSD schema. I have seen many examples where you inject the XSD in to the XML and then validate the XML. I don't want to change the XML, is it possible to validate the XML, against the schema without change the XML?

这很容易在C#中的几行代码

It's easy to code with a few lines in C#.

我创建了一个简单的命令行界面实用程序,有两个参数:XML,XSD和做验证

I created a simple command line interface utility that takes two parameters: XML, XSD and does verification.

您可以下载这里。一>

下面是主要代码:

            // 1- Read XML file content
            reader = new XmlTextReader(XMLPath);

            // 2- Read Schema file content
            StreamReader SR = new StreamReader(XSDPath);

            // 3- Create a new instance of XmlSchema object
            XmlSchema Schema = new XmlSchema();
            // 4- Set Schema object by calling XmlSchema.Read() method
            Schema = XmlSchema.Read(SR,
                new ValidationEventHandler(ReaderSettings_ValidationEventHandler));

            // 5- Create a new instance of XmlReaderSettings object
            XmlReaderSettings ReaderSettings = new XmlReaderSettings();
            // 6- Set ValidationType for XmlReaderSettings object
            ReaderSettings.ValidationType = ValidationType.Schema;
            // 7- Add Schema to XmlReaderSettings Schemas collection
            ReaderSettings.Schemas.Add(Schema);

            // 8- Add your ValidationEventHandler address to
            // XmlReaderSettings ValidationEventHandler
            ReaderSettings.ValidationEventHandler +=
                new ValidationEventHandler(ReaderSettings_ValidationEventHandler);

            // 9- Create a new instance of XmlReader object
            XmlReader objXmlReader = XmlReader.Create(reader, ReaderSettings);


            // 10- Read XML content in a loop
            while (objXmlReader.Read())
            { /*Empty loop*/}