使用xsd验证xml文件是否规范

 public class XmlValidation
    {
        StringBuilder sb;
        public  string XmlValidationByXsd(string XmlPath,string XsdPath)
        {
            sb= new StringBuilder();
            string strReturnValue = string.Empty;
            
            string dataFile = XmlPath;
            string schemaFile = XsdPath;
	    //备注:这里为xsd验证文件里的命名空间targetNamespace
            string namespaceUrl = "http://www.xxx.cn/xxx";

            XmlReaderSettings settings = new XmlReaderSettings();
            settings.ValidationType = ValidationType.Schema;
            settings.Schemas.Add(namespaceUrl, schemaFile);
            settings.ValidationEventHandler += new ValidationEventHandler(settings_ValidationEventHandler);

            string errorMessage = "这不是一个合乎规范的数据文件";
            
            XmlReader reader = XmlReader.Create(dataFile, settings);
            try
            {
                reader.MoveToContent();
                while (reader.Read())
                {
                    if (reader.NodeType == XmlNodeType.Document && reader.NamespaceURI != namespaceUrl)
                    {
                        strReturnValue = errorMessage;
                        break;
                    }
                }
            }
            catch (XmlException ex)
            {
                sb.AppendFormat("{0}\n", ex.Message);
            }
            finally
            {
                reader.Close();
            }

            if (sb.Length == 0)
                strReturnValue = "true";
            else
                strReturnValue = sb.ToString();

            return strReturnValue;
        }

         void settings_ValidationEventHandler(object sender, System.Xml.Schema.ValidationEventArgs e)
        {
            sb.AppendFormat("{0}\n", e.Message);
        }
    }


你可能感兴趣的:(使用xsd验证xml文件是否规范)