关于用XSD文件验证字符串型的XML中特定的字段只能是几个值

来源:互联网 发布:混沌战域仙翼进阶数据 编辑:程序博客网 时间:2024/05/16 11:19

1.我们的目标是验证一个XML中的字段中的ActionType只能是DispensedCardPurchase,NonDispensedCardPurchase,DispensedFixedValueCardPurchase,NonDispensedFixedValueCardPurchase,这几个值,

XSD文件:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema targetNamespace=

"http://schemas.datacontract.org/2004/07/Coinstar.Coin.Server.Conductor.Transaction.DataContract"
    elementFormDefault="qualified"       
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
<xs:element name="ExtendConfiguration">
   <xs:complexType>     
      <xs:sequence>
        <xs:element name="ActionType">
          <xs:simpleType>
            <xs:restriction base="xs:string">
              <xs:enumeration value="DispensedCardPurchase"/>
              <xs:enumeration value="NonDispensedCardPurchase"/>
              <xs:enumeration value="DispensedFixedValueCardPurchase"/>
              <xs:enumeration value="NonDispensedFixedValueCardPurchase"/>
            </xs:restriction>
          </xs:simpleType>
        </xs:element>  
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>
2.我们的程序中用C#代码和XSD文件来对XML文件进行验证

C#:

string ErrString = string.Empty;
        public bool CheckXmlValidate(string strRequestXML)
        {
            StringReader sRead = null;
            XmlReader xmlRead = null;
            XmlSchemaSet schemaSet;
            FortressAppSettings fortressAppSettings = new FortressAppSettings();
            string filePath = fortressAppSettings.GetAppSetting("LoadCardPath");
            try
            {
                schemaSet = new XmlSchemaSet();
                sRead = new StringReader(strRequestXML);
                schemaSet.Add(null, filePath);

                XmlReaderSettings settings = new XmlReaderSettings();
                settings.ValidationEventHandler += new ValidationEventHandler(ValidationEventCallBack);
                settings.ValidationType = ValidationType.Schema;
                settings.Schemas = schemaSet;

                xmlRead = XmlReader.Create(sRead, settings);
                while (xmlRead.Read())
                {

                }

                bool result;
                result = ErrString.ToString() == String.Empty ? true : false;

                return result;
            }
            catch
            {
                throw new Exception("Data Struct is not correct");
            }
            finally
            {
                if (xmlRead != null)
                {
                    xmlRead.Close();
                }
            }
        }

        private void ValidationEventCallBack(Object sender, ValidationEventArgs e)
        {
            if (e.Severity == XmlSeverityType.Warning)
            {
                ErrString = string.Empty;
            }
            else
            {
                ErrString = "Err:" + e.Message;
            }
        }

 

3.为了清晰期间,我给一个能够符合验证的XML字符串和一个不符合的XML字符串

符合的:

string requestXml = "<ExtendConfiguration  xmlns=\"http://schemas.datacontract.org/2004/07/Coinstar.Coin.Server.Conductor.Transaction.DataContract\">

<ActionType>DispensedCardPurchase</ActionType></ExtendConfiguration>";

不符合的:

requestXml = "<ExtendConfiguration  xmlns=\"http://schemas.datacontract.org/2004/07/Coinstar.Coin.Server.Conductor.Transaction.DataContract\">

<ActionType>DispensedPurchase</ActionType></ExtendConfiguration>";

会出现异常的:

string requestXml = "<ExtandConfiguration  xmlns=\"http://schemas.datacontract.org/2004/07/Coinstar.Coin.Server.Conductor.Transaction.DataContract\">

<ActionType>DispensedCardPurchase</ActionType></ExtendConfiguration>"

原创粉丝点击