0

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 minOccures=1 , when i'm running this code it's validating and showing error message =0 but i expecting error of not supplying <UserLoginName> value.

1 Answer 1

3

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

<TXLife xmlns="http://tempuri.org/NewApplicationSchema.xsd">
   ...
</TXLife>
Sign up to request clarification or add additional context in comments.

3 Comments

XML is user input, so how can validate that one without schema?
Not sure that it will work, but try schemas.Add("", _sourceXsd);
If the XML document does not contain the namespace defined in the targetnamespace attribute within the schema it will not validate correctly. So you have 2 options change the schema or change the XML. You could change the XML in code once it is loaded, forcing the namespace into root element.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.