这不是C#代码吗?发到这里来了根据schema文件验证一个xml文件是不是合法.

解决方案 »

  1.   

    XML校验,MSDN验证XML数据 
      

  2.   

    xsd 去验证 xml合法性
    我给你个例子 一看就懂:。
    public class XmlInPutDateFeedRequestValidator
        {
            public List<ValidationEventArgs> errorArgs = null;        public void Validate()
            {
                //Load schema.  
                XmlSchemaCollection xsc = new XmlSchemaCollection();
                xsc.Add("urn:InPutDateFeedRequest-schema", "InPutDateFeedRequest.xsd");            // Validate
                string fileName = "InPutDateFeedRequest.xml";
                XmlInPutDateFeedRequestValidator rr = new XmlInPutDateFeedRequestValidator();
                bool error = ValidateXml(fileName, xsc);            if (!error)
                {
                    Console.WriteLine(string.Format("\n The xml file {0} right!", fileName));
                }
                else
                {
                    Console.WriteLine(string.Format("\n The xml file {0} has error!", fileName));
                    foreach (ValidationEventArgs arg in errorArgs)
                    {
                        Console.WriteLine("Message:" + errorArgs);
                    }
                }
                Console.ReadLine();
            }        public bool ValidateXml(String filename, XmlSchemaCollection xsc)
            {
                Console.WriteLine();
                XmlTextReader reader = new XmlTextReader(filename);
                XmlValidatingReader vreader = new XmlValidatingReader(reader);
                vreader.ValidationType = ValidationType.Schema;
                vreader.Schemas.Add(xsc);
                vreader.ValidationEventHandler += new ValidationEventHandler(ValidationHandler);
                errorArgs = new List<ValidationEventArgs>();
                while (vreader.Read())
                {
                }
                vreader.Close();
                return errorArgs.Count > 0;
            }        public void ValidationHandler(object sender, ValidationEventArgs args)
            {
                //Determine whether an error
                if (args.Severity == XmlSeverityType.Error)
                {
                    errorArgs.Add(args);
                }
                else
                {
                    errorArgs.Add(args);
                }
            }