简体   繁体   中英

XML validation Using XSD Not Working in C#

i have an XML, i need to validate it's not and the sequence, I've written some code but it's not working.

XML

<?xml version="1.0" encoding="utf-8" ?>

<TXLife>
  <UserAuthRequest>
    <UserPswd>
      <CryptType>NONE</CryptType>
      <Pswd/>
    </UserPswd>
    <VendorApp>
      <VendorName>AAA</VendorName>
      <AppName>BBB</AppName>
    </VendorApp>
  </UserAuthRequest>
</TXLife>

XSD

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="XMLSchema1"
    targetNamespace="http://tempuri.org/NewApplicationSchema.xsd"
    elementFormDefault="qualified"
    xmlns="http://tempuri.org/XMLSchema1.xsd"
    xmlns:mstns="http://tempuri.org/XMLSchema1.xsd"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
  <xs:element name="TXLife">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="UserAuthRequest" minOccurs="1">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="UserLoginName" type="xs:string" minOccurs="1"></xs:element>
              <xs:element name="UserPswd">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="CryptType" type="xs:string"></xs:element>
                    <xs:element name="Pswd"></xs:element>
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
              <xs:element name="VendorApp">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="VendorName">
                      <xs:complexType>
                        <xs:attribute name="VendorCode" type="xs:int"></xs:attribute>
                      </xs:complexType>
                    </xs:element>
                    <xs:element name="AppName" type="xs:string"></xs:element>
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
      <xs:attribute name="VendorCode" type="xs:int"></xs:attribute>
    </xs:complexType>
  </xs:element>

</xs:schema>

C# code:

public void ValidateXML1()
        {
            List<string> _errors = new List<string>();
            try
            {
                XmlSchemaSet schemas = new XmlSchemaSet();
                schemas.Add("http://tempuri.org/NewApplicationSchema.xsd", _sourceXsd);

                XDocument doc = XDocument.Load(_sourceXml);
                string msg = "";
                doc.Validate(schemas, (o, e) =>
                {
                    _errors.Add(e.Message);
                    msg += e.Message + Environment.NewLine;
                });
            }
            catch (Exception e)
            {

            }

        }

As you can see that I've not supplied the <UserLoginName> in XML but in my XSD it's m inOccures=1 , when i'm running this code it's validating and showing error message =0 but i expecting error of not supplying <UserLoginName> value.

Your XML file is missing the schema namespace. Change it to

<TXLife xmlns="http://tempuri.org/NewApplicationSchema.xsd">
   ...
</TXLife>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM