XML Signature element is not declared
Asked Answered
K

2

11

I created XSD using Visual StudioXML Tools. And I use following C# code to validate XML and facing this error.

Error

The element is not declared 'http://www.w3.org/2000/09/xmldsig#:Signature'.

So my question is how to fix it because in edit mode XML is valid 100%?

Thank you!

C#

  private void buttonValidateXML_Click(object sender, EventArgs e)
        {
            try
            {            
                bool result = IsValidXml2(textBoxSignedXML.Text, textBoxXSDFile.Text, "");
                rtbValidationResult.Text = result.ToString();
            }
            catch (Exception ex)
            {
                rtbValidationResult.Text = ex.Message;
            }
        }

public static bool IsValidXml2(string xmlFilePath, string xsdFilePath, string namespaceName)
        {
            var xdoc = XDocument.Load(xmlFilePath);
            var schemas = new XmlSchemaSet();
            schemas.Add(namespaceName, xsdFilePath);

            bool result = true;
            xdoc.Validate(schemas, (sender, e) =>
            {
                result = false;
            });

            return result;
        }

XML

<?xml version="1.0" encoding="utf-8"?>
<Envelope version="1">
  <Deposit>
    <ClientId>1234567890123</ClientId>
    <Account>0045678</Account>
    <Currency>USD</Currency>
    <Total>5000.00</Total>
    <SignedDate>2016-02-15</SignedDate>     
    <Cheques>
      <Cheque>
        <Images>
          <Front>
            SUkqAAgAAAAPAP4ABAABAAAAAAAAAAABBAABAAAAfQUAAAEBBAABAAAAWgIAAAIBAwABAAAAAQAA
            AAMBAwABAAAABAAAAAYBAwABAAAAAAAAAA4BAgAhAAAA0AAAABEBBAABAAAAAAIAABIBAwABAAAA
            AQAAABUBAwABAAAAAQAAABYBBAABAAAAWgIAABcBBAABAAAAvi8AABoBBQABAAAAAAEAABsBBQAB
            EdF0fRG0R0YRdH2R2XzaI6OxRQAQAQA=
          </Front>
          <Back>
            SUkqAAgAAAAOAP4ABAABAAAAAAAAAAABBAABAAAAgQUAAAEBBAABAAAAVAIAAAIBAwABAAAAAQAA
            AAMBAwABAAAABAAAAAYBAwABAAAAAAAAABEBBAABAAAAAAEAABIBAwABAAAAAQAAABUBAwABAAAA
            AQAAABYBBAABAAAAVAIAABcBBAABAAAAcggAABoBBQABAAAAwAAAABsBBQABAAAA0AAAACgBAwAB
            AAAAAgAAAAAAAAAAAAAAAAAAAAAAyAAAAAEAAAAAAAAAAAAAAMgAAAABAAAAAAAAAAAAAAAAAAAA
            AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAP//LZCdO1f7iRrKXTqt//pePx///9lNoL487Ajh
            jFyzUrtwg+6gg9VmvCIWA42XMwziSUEEw7GoIcSUWZ3Y0oKmGR3LToGVC2LhkK6H4sorQNRUMLjH
            LTGzlpv3RFCcH4NLB9hvLTmD8tMgOsG+WVaR5AweTcEWMMfaDQxDIx5NwVQx8OMPeGFHLSUlLcSS
            1JtNV/9/rrffuO+h9bx////kfvABABAA
          </Back>
        </Images>
        <MicrCodeCmc>123456789012345678901234567890</MicrCodeCmc>
        <Amount>465.22</Amount>
        <PaymentDate>2016-02-15</PaymentDate>
        <EmissionDate>2016-02-15</EmissionDate>
      </Cheque>
      <Cheque>
        <Images>
          <Front>
            SUkqAAgAAAAPAP4ABAABAAAAAAAAAAABBAABAAAAfQUAAAEBBAABAAAAWgIAAAIBAwABAAAAAQAA
            AAMBAwABAAAABAAAAAYBAwABAAAAAAAAAA4BAgAhAAAA0AAAABEBBAABAAAAAAIAABIBAwABAAAA
            AQAAABUBAwABAAAAAQAAABYBBAABAAAAWgIAABcBBAABAAAAvi8AABoBBQABAAAAAAEAABsBBQAB
            EdF0fRG0R0YRdH2R2XzaI6OxRQAQAQA=
          </Front>
          <Back>
            SUkqAAgAAAAOAP4ABAABAAAAAAAAAAABBAABAAAAgQUAAAEBBAABAAAAVAIAAAIBAwABAAAAAQAA
            AAMBAwABAAAABAAAAAYBAwABAAAAAAAAABEBBAABAAAAAAEAABIBAwABAAAAAQAAABUBAwABAAAA
            AQAAABYBBAABAAAAVAIAABcBBAABAAAAcggAABoBBQABAAAAwAAAABsBBQABAAAA0AAAACgBAwAB
            AAAAAgAAAAAAAAAAAAAAAAAAAAAAyAAAAAEAAAAAAAAAAAAAAMgAAAABAAAAAAAAAAAAAAAAAAAA
            AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAP//LZCdO1f7iRrKXTqt//pePx///9lNoL487Ajh
            jFyzUrtwg+6gg9VmvCIWA42XMwziSUEEw7GoIcSUWZ3Y0oKmGR3LToGVC2LhkK6H4sorQNRUMLjH
            LTGzlpv3RFCcH4NLB9hvLTmD8tMgOsG+WVaR5AweTcEWMMfaDQxDIx5NwVQx8OMPeGFHLSUlLcSS
            1JtNV/9/rrffuO+h9bx////kfvABABAA
          </Back>
        </Images>
        <MicrCodeCmc>123456789012345678901234567890</MicrCodeCmc>
        <Amount>99999999999</Amount>
        <PaymentDate>2016-02-15</PaymentDate>
        <EmissionDate>2016-02-15</EmissionDate>
      </Cheque>
    </Cheques>
  </Deposit>   
  <Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
    <SignedInfo>
      <CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" />
      <SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1" />
      <Reference URI="">
        <Transforms>
          <Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" />
        </Transforms>
        <DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
        <DigestValue>TVO2Gssf5TUdXFYG/PrHDyqYFqs=</DigestValue>
      </Reference>
    </SignedInfo>
    <SignatureValue>KwQInN03ywa0u0t4HedxgE1fOU7XYLQScKuQ6vdYoIZME5Hm5jpFeX2ORA2U+BO7JNjmFilTW05VntS3k98YCZhNXH9Iw/YEC1nw4JJLzygYbbCftkiY5v5+b494mQPryCtscwTtbziW6QilILSFDGmco2JopRfVe+qfdN/JyB1HXhUfApyNEsw/cJLj6aaz5ivN1sLFgAlikbwCNpF+mRnZY5u7/S8uT8WhEyK32EcatdjzKbP0PwnIlumhOpUMerWeLZ7neuJq6R/IuFgZ1Y5U6ppyuOjhtiHp4glC/uNUS/y7jMzG29thWBkEtSE9AcEt2IZ0HOEZE3kdFXufjA==</SignatureValue>
  </Signature>
</Envelope>

XSD

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:tns="http://example.com/ct-required"
           xmlns:xmime="http://www.w3.org/2005/05/xmlmime" >

  <xs:import namespace="http://www.w3.org/2005/05/xmlmime"
            schemaLocation="http://www.w3.org/2005/05/xmlmime"/>

    <xs:import namespace="http://www.w3.org/2000/09/xmldsig#" schemaLocation="xmldsig-core-schema.xsd"/>

  <xs:simpleType name="PNGPictureType"
          xmime:expectedContentTypes="image/png">
    <xs:restriction base="xs:base64Binary"/>
  </xs:simpleType>

  <xs:simpleType name="Money">
    <xs:restriction base="xs:decimal">
      <xs:totalDigits value="13" />
      <xs:fractionDigits value="2" />
      <xs:minInclusive value="0.00" />
      <xs:maxInclusive value="99999999999.99" />
    </xs:restriction>
  </xs:simpleType>

  <xs:simpleType name="Currency">
        <xs:annotation>
            <xs:documentation>Currency Code: ISO 4217</xs:documentation>
        </xs:annotation>
        <xs:restriction base="xs:string">
            <xs:enumeration value="USD" />
            <xs:enumeration value="UYU" />
            <xs:minLength value="3" />
            <xs:maxLength value="3" />
        </xs:restriction>
    </xs:simpleType>

  <xs:element name="Envelope">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Deposit" minOccurs="1" maxOccurs="1">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="ClientId" type="xs:unsignedLong" />
              <xs:element name="Account" type="xs:unsignedLong" />
              <xs:element name="Currency" type="Currency" />
              <xs:element name="Total" type="Money" />
              <xs:element name="SignedDate" type="xs:date" />
              <xs:element name="Cheques">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element maxOccurs="unbounded" name="Cheque">
                      <xs:complexType>
                        <xs:sequence>
                          <xs:element name="Images">
                            <xs:complexType>
                              <xs:sequence>
                                <xs:element name="Front" type="PNGPictureType" />
                                <xs:element name="Back" type="PNGPictureType" />
                              </xs:sequence>
                            </xs:complexType>
                          </xs:element>
                          <xs:element name="MicrCodeCmc" type="xs:string" />
                          <xs:element name="Amount" type="Money" />
                          <xs:element name="PaymentDate" type="xs:date" />
                          <xs:element name="EmissionDate" type="xs:date" />
                        </xs:sequence>
                      </xs:complexType>
                    </xs:element>
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
        <xs:element xmlns:q1="http://www.w3.org/2000/09/xmldsig#" ref="q1:Signature" />
      </xs:sequence>
      <xs:attribute name="version" type="xs:unsignedByte" use="required" />
    </xs:complexType>
  </xs:element>
</xs:schema>

UPDATE #1

I have tried different approaches but no joy.

enter image description here

And even if I reference XSD to local file it does not work. I see the same error.

 <xs:import namespace="http://www.w3.org/2000/09/xmldsig#" schemaLocation="http://www.w3.org/TR/2002/REC-xmldsig-core-20020212 file:///C:/Temp/xmldsig-core-schema.xsd"/>

UPDATE #2

Even I use this approach no joy.

 public static bool IsValidXml1(string xmlFilePath, string xsdFilePath, string namespaceName)
        {
            XDocument xdoc = null;
            var settings = new XmlReaderSettings();
            settings.DtdProcessing = DtdProcessing.Ignore;
            settings.ProhibitDtd = false;

            try
            {
                using (XmlReader xr = XmlReader.Create(xmlFilePath, settings))
                {
                    xdoc = XDocument.Load(xr);
                    var schemas = new XmlSchemaSet();
                    schemas.Add(namespaceName, xsdFilePath);
                    schemas.Add(@"http://www.w3.org/2000/09/xmldsig#", @"D:\Temp\xmldsig-core-schema.xsd");
                    xdoc.Validate(schemas, null);

                    return true;
                }                
            }
            catch (XmlSchemaValidationException ex)
            {
                // throw;
            }

            return false;
        }
Kuth answered 24/2, 2017 at 14:4 Comment(6)
@HenkHolterman Well... I have to use XSD file to validate XML...Kuth
@dimi - you are applying the XmlReader fix to the wrong input file.Schoenfeld
@HenkHolterman: If you provide another mechanism to resolve the namespace beyond what a complete xs:import would do, then you can succeed in correcting the problem without changing the XSD. However, the most interoperable fix would be to complete the xs:import as I've shown; then, OP and other parties will be able to validate with just the XML and XSD, without an additional mechanism to bind the imported XSD to the namespace. If you could add a specific XmlReader fix to the XSD fix, the total problem may be resolved.Japeth
@HenkHolterman you might be interested in my solution since it avoids your kludge with modification of dsig xsd.Cannonball
I already figured that out - and pointed the OP to how to fix it. You don't need the outer XmlReader, just the second one.Schoenfeld
@HenkHolterman well I think I read all comments and did not find it anywhereCannonball
C
12

If you don't want to change anything to xsd or xml - do the following:

  1. (optional) Download xsd from w3 site and save to local disk. W3 site is VERY slow because a lot of software worldwide constantly request those schemas. If you will use that xsd directly - you will often fail by timeout. Some validation tools already have such schemas cached locally, but not .NET validator.

  2. Modify your validation method from UPDATE 2 the following way:

    public static bool IsValidXml1(string xmlFilePath, string xsdFilePath, string namespaceName)
    {
        XDocument xdoc = null;
        var settings = new XmlReaderSettings();
        settings.DtdProcessing = DtdProcessing.Ignore;
    
        try
        {
            using (XmlReader xr = XmlReader.Create(xmlFilePath, settings))
            {
                xdoc = XDocument.Load(xr);
                var schemas = new XmlSchemaSet();                    
                schemas.Add(namespaceName, xsdFilePath);                   
                using (var fs = File.OpenRead(@"D:\Temp\xmldsig-core-schema.xsd")) 
                using (var reader = XmlReader.Create(fs, new XmlReaderSettings() {
                    DtdProcessing = DtdProcessing.Ignore // important
                })) {
                    schemas.Add(@"http://www.w3.org/2000/09/xmldsig#", reader);
                }
    
                xdoc.Validate(schemas, null);
    
                return true;
            }
        }
        catch (XmlSchemaValidationException ex)
        {
            // throw;
        }
    
        return false;
    }
    

You have to add that schema using XmlReader and not directly, because if you add directly (like in your update 2) - it will fail to parse DTD block, because when you add XmlSchema to XmlSchemaSet using url (or file path) - it will read that file using XmlReaderSettings with DtdProcessing = DtdProcessing.Prohibit. We need to change that to DtdProcessing.Ignore or DtdProcessing.Parse. After that your validation method will work fine for target xsd and xml file, without any changes (and will correctly fail in case xml does not match xsd).

Cannonball answered 1/3, 2017 at 19:23 Comment(1)
It works from the first attempt! Thanks, bro! God bless you!Kuth
J
10

You are very close. In your XSD, simply replace,

<xs:import namespace="http://www.w3.org/2000/09/xmldsig#" />

with

<xs:import namespace="http://www.w3.org/2000/09/xmldsig#" 
           schemaLocation=
    "http://www.w3.org/TR/2002/REC-xmldsig-core-20020212/xmldsig-core-schema.xsd"/>

and your XSD will no longer have that error, and your XML will be valid against your XSD.

Explanation

XSDs can be composed via xs:import and xs:include. In both cases, the location of the referenced XSD has to be specified with a required schemaLocation attribute, which was missing in OP's original XSD. By adding xs:import/@schemaLocation as shown above, the error is eliminated.


Update #1:

When you switched to use a local XSD, you made a mistake in your xs:import:

Change

 <xs:import namespace="http://www.w3.org/2000/09/xmldsig#"
            schemaLocation="http://www.w3.org/TR/2002/REC-xmldsig-core-20020212 
                            file:///C:/Temp/xmldsig-core-schema.xsd"/>

to

 <xs:import namespace="http://www.w3.org/2000/09/xmldsig#"
            schemaLocation="file:///C:/Temp/xmldsig-core-schema.xsd"/>

(You were following an example for @xsi:schemaLocation in XML documents which has namespace-location pairs; xs:import/@schemaLocation is different.)


Update #2:

So my question is how to fix it because in edit mode XML is valid 100%?

Perhaps this is the disconnect. Editing an XML file in Visual Studio does not automatically validate it against an XSD. You need to do that in code or via a validating XML editor such Oxygen XML Editor or XML Spy.

Also, your C# validation code may have issues. See Validating an XML against referenced XSD in C#

Japeth answered 24/2, 2017 at 14:20 Comment(12)
Can you explain? I've never had to do this.Schoenfeld
I am sorry but it does not work. It says: Warning Imported schema for namespace w3.org/2000/09/xmldsig# was not resolvedKuth
@Dimi: The above correction is necessary to eliminate the error in your XSD, and once made, your XML is valid according to your corrected XSD. I have confirmed this independent of your code. I suggest that you validate your XML with an independent XML editor against your XSD to be sure that you've not made a mistake updating your XSD.Japeth
Please take a look at my question I just added a new screenshot with this warning.Kuth
Enter http://www.w3.org/TR/2002/REC-xmldsig-core-20020212/xmldsig-core-schema.xsd into your browser and make sure that you have connectivity to the schema location. If it does not come up, or if it takes excessively long and is causing time-outs, then that is the problem.Japeth
Hmmm... It opens really fast under a browser. Would you mind to suggest a syntax to reference to local xmldsig-core-schema.xsd file please? Should it be like <xs:import namespace="w3.org/2000/09/xmldsig#" schemaLocation="xmldsig-core-schema.xsd"/>Kuth
See How to reference a local XML Schema file correctlyJapeth
Hey... I just tried the local file and it does not work. Please see my updated question. Would you mind to help me please?Kuth
With all due respect to your answer and comments, would you mind to help me to resolve this issue? I have tried ALL OPTIONS but nothing is working.Kuth
Answer updated. Last tip: Strongly suggest that you first get your XML and XSD correct as I've shown, then worry about your C# code. I have shown you exactly what you need to do to fix your XSD so your XML will validate. I need to move on -- you've asked a whole series of follow-ups, and this site is not intended for ongoing exchanges. Use separate questions for any future issues. Thanks, and good luck.Japeth
Answer updated. My approach does resolve the error you report ("The element is not declared...") but you may have additional problems with your C# code. See Validating an XML against referenced XSD in C# for how to validate XML against an XSD in C#.Japeth
Hi. You know the idea of XSD is validate XML. That is why I ineed a full answer. I need implement correct validation for XML and XSD I have got. If you are able to help me then you will gain 500. Thank you!Kuth

© 2022 - 2024 — McMap. All rights reserved.