xsd 进行判断config是否符合规则

//----------------------------------------------------------------
// <copyright file="ConfigFileLoader.cs" company="Microsoft">
//    Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
// <summary>Check and load configuration file.</summary>
//----------------------------------------------------------------
namespace Microsoft.Msn.Tokyo.C3.Ifw.IO
{
    using System;
    using System.Collections.ObjectModel;
    using System.IO;
    using System.Xml;
    using System.Xml.Linq;
    using System.Xml.Schema;
    using Microsoft.Msn.Tokyo.C3;
    using Microsoft.Msn.Tokyo.C3.Config;

    /// <summary>
    /// Check and load configuration file.
    /// </summary>
    public static class ConfigFileLoader
    {
        /// <summary>
        /// Check and load configuration file.
        /// </summary>
        /// <param name="configFilePath">The fully qualified name of the configuration file, or the relative configuration file name.</param>
        /// <param name="xmlSchema">Xml schema for the configuration file.</param>
        /// <returns>config contents</returns>
        /// <exception cref="Microsoft.Msn.Tokyo.C3.Config.ConfigFileNotFoundException">The config file specified by path does not exist. </exception>
        /// <exception cref="Microsoft.Msn.Tokyo.C3.Config.C3ConfigurationException">Invalid config file.</exception>
        public static XmlDocument LoadConfig(string configFilePath, string xmlSchema)
        {
            try
            {
                /* ファイルの存在チェック */
                if (!File.Exists(configFilePath))
                {
                    throw new ConfigFileNotFoundException(configFilePath);
                }

                /* Xsdをリソースから読み込み */
                XmlSchemaSet schemeSet = new XmlSchemaSet();
                using (StringReader reader = new StringReader(xmlSchema))
                {
                    schemeSet.Add(null, XmlReader.Create(reader));
                }

                XDocument doc = XDocument.Load(configFilePath);
                Collection<InvalidConfigValueException> validationExceptions = new Collection<InvalidConfigValueException>();
                /* Xmlの妥当性チェック */
                doc.Validate(
                    schemeSet,
                    (sender, e) =>
                    {
                        XObject xo = sender as XObject;
                        if (xo != null)
                        {
                            /* ノードの場合 */
                            if (xo.NodeType == XmlNodeType.Element)
                            {
                                XElement xe = xo as XElement;
                                if (xe != null)
                                {
                                    validationExceptions.Add(new InvalidConfigValueException(xe.Name.ToString(), string.Empty, e.Exception));
                                }
                            }

                            /* 属性の場合 */
                            else if (xo.NodeType == XmlNodeType.Attribute)
                            {
                                XAttribute xa = xo as XAttribute;
                                if (xa != null)
                                {
                                    validationExceptions.Add(new InvalidConfigValueException(xo.Parent.Name.ToString(), xa.Value, e.Exception));
                                }
                            }

                            /* ノード・属性以外の場合 */
                            else
                            {
                                validationExceptions.Add(new InvalidConfigValueException(e.Message, e.Exception));
                            }
                        }
                    },
                    true);

                /* スキーマエラーがある場合 */
                if (validationExceptions.Count > 0)
                {
                    string message = string.Empty;

                    foreach (InvalidConfigValueException e in validationExceptions)
                    {
                        message += e.ToString() + Environment.NewLine;
                    }

                    throw new C3ConfigurationException(message, 4);
                }

                /* XDocument⇒XmlDocument型変換 */
                XmlDocument xmlDocument = new XmlDocument();
                using (XmlReader reader = doc.CreateReader())
                {
                    reader.MoveToContent();
                    xmlDocument.Load(reader);
                }

                return xmlDocument;
            }
            catch (Exception ex)
            {
                if (ex is C3Exception)
                {
                    throw;
                }
                else
                {
                    throw new C3Exception(null, ex);
                }
            }
        }
    }
}
<?xml version="1.0" encoding="utf-8" ?>
<!--isCheck:1 start check thread. checkTime:check thread start every second value. waitRemoverTime:force kill Process until commit over. killRemoverTime:force kill Process    -->
<Remover isCheck="1" checkTime="300" waitRemoveTime="150000" killRemoveTime="153000" threadNum="4" >
    <Items>
        <!-- get unnecessary image count-->
        <Item isErrorRollback="true">
            <ExecuteProcedure name="spAutosRemoverGetUnnecessaryImageCount" dbConnectionKeyName="BackEndC3ChannelDBConnectionKey" return="List1">
            </ExecuteProcedure>
        </Item>
        <!-- 中古車物件画像削除処理-->
        <Item isErrorRollback="true">
            <ExecuteProcedure name="spAutosRemoverGetUnnecessaryUsedcarImageFilePath" dbConnectionKeyName="BackEndC3ChannelDBConnectionKey" return="List1">
                <Parameter key="@prmCount" value="120000"/>
                <Parameter key="@prmCommitCount" value="1000"/>
            </ExecuteProcedure>
            <DeleteFile removeObjectKey="List1:0" isDeleteTarget="true" rootDirectry="%ImageStorePath%\ca006" isDirectry="false" backupTo="" />
        </Item>
        <!--中古車複数点画像削除処理-->
        <Item isErrorRollback="true">
            <ExecuteProcedure name="spAutosRemoverGetUnnecessaryImageFilePath" dbConnectionKeyName="BackEndC3ChannelDBConnectionKey" return="List2">
                <Parameter key="@prmCount" value="200000"/>
                <Parameter key="@prmCommitCount" value="1000"/>
            </ExecuteProcedure>
            <DeleteFile removeObjectKey="List2:0" isDeleteTarget="true" rootDirectry="%ImageStorePath%\ca007" isDirectry="false" backupTo="" />
        </Item>
        <!--販売店画像削除処理-->
        <Item isErrorRollback="false">
            <ExecuteProcedure name="spAutosRemoverGetCustomerCode" dbConnectionKeyName="BackEndC3ChannelDBConnectionKey" return="List3"/>
            <DeleteFile removeObjectKey="List3:0" isDeleteTarget="false" rootDirectry="%ImageStorePath%\ca009" isDirectry="true" backupTo="" />
        </Item>
        <!--DBに物件画像削除情報の物理削除処理-->
        <Item isErrorRollback="true">
            <ExecuteProcedure name="spAutosRemoverDeleteUnnecessaryImageRecord" dbConnectionKeyName="BackEndC3ChannelDBConnectionKey" >
                <Parameter key="@deletePreviousDays" value="60" />
            </ExecuteProcedure>
        </Item>
        <!-- ジョブワークファイル削除処理-->
        <Item isErrorRollback="false">
            <DeleteFile saveDays="10" isDeleteTarget="true" rootDirectry="%WorkFolderRootPath%\AU001" isDirectry="false" backupTo="" />
            <DeleteFile saveDays="7" isDeleteTarget="true" rootDirectry="%WorkFolderRootPath%\AU002" isDirectry="false" backupTo="" />
        </Item>
        <!-- 反響ログファイル削除処理-->
        <Item isErrorRollback="true">
            <ExecuteProcedure name="spAutosRemoverUpdateAutosFileHistoryStatus" dbConnectionKeyName="C3CommonConnectionStringKey" return="List4"/>
            <DeleteFile removeObjectKey="List4:0" isDeleteTarget="true" rootDirectry="%VirtualFileRootPath%" isDirectry="false" backupTo="" />
        </Item>
    </Items>
</Remover>
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="Remover" type="RemoverType">
    </xs:element>
    <xs:complexType name="RemoverType">
        <xs:sequence>
            <xs:element name="Items" minOccurs="1" maxOccurs="1">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element name="Item" minOccurs="1" maxOccurs="unbounded">
                            <xs:complexType>
                                <xs:choice minOccurs="0" maxOccurs="unbounded">
                                    <xs:element name="ExecuteProcedure">
                                        <xs:complexType>
                                            <xs:sequence>
                                                <xs:element name="Parameter" minOccurs="0" maxOccurs="unbounded">
                                                    <xs:complexType>
                                                        <xs:attribute name="value" type="xs:string" use="required" />
                                                        <xs:attribute name="key" type="xs:string" use="required" />
                                                    </xs:complexType>
                                                </xs:element>
                                            </xs:sequence>
                                            <xs:attribute name="name" type="xs:string" use="required" />
                                            <xs:attribute name="dbConnectionKeyName" type="xs:string" use="required" />
                                            <xs:attribute name="return" type="xs:string" use="optional" />
                                        </xs:complexType>
                                    </xs:element>
                                    <xs:element name="DeleteFile">
                                        <xs:complexType>
                                            <xs:attribute name="removeObjectKey" type="xs:string" use="optional" />
                                            <xs:attribute name="saveDays" type="xs:string" use="optional" />
                                            <xs:attribute name="isDeleteTarget" type="xs:boolean" use="required" />
                                            <xs:attribute name="rootDirectry" type="xs:string" use="optional" />
                                            <xs:attribute name="isDirectry" type="xs:boolean" use="required" />
                                            <xs:attribute name="backupTo" type="xs:string" use="optional" />
                                        </xs:complexType>
                                    </xs:element>
                                </xs:choice>
                                <xs:attribute name="isErrorRollback" type="xs:boolean" use="optional" />
                            </xs:complexType>
                        </xs:element>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
        <xs:attribute name="isCheck" type="xs:string" use="required"></xs:attribute>
        <xs:attribute name="checkTime" type="xs:string" use="required"></xs:attribute>
        <xs:attribute name="waitRemoveTime" type="xs:string" use="required"></xs:attribute>
        <xs:attribute name="killRemoveTime" type="xs:string" use="required"></xs:attribute>
        <xs:attribute name="threadNum" type="xs:string" use="required"></xs:attribute>
    </xs:complexType>
</xs:schema>
原文地址:https://www.cnblogs.com/2814/p/2812955.html