do you like puzzles to resolve? This is a big one!
First, I made a lot of researches for the solution, but, my situation is still very complicated.
Well, I have a XML that is validated in a complex XSD file, but, inside that XML I also have another complex type that is for the digital signature (xmlsignature).
My XSD is:
<xsd:element name="EnviarLoteRpsEnvio">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="LoteRps" type="tcLoteRps"/>
<xsd:element ref="dsig:Signature" minOccurs="0" maxOccurs="1"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
The LoteRps (imagine a class like TPerson) is a complex type, and below the LoteRps I have my problem, the element dsig:Signature
To validate the LoteRps I have my XSD file (as described), but, to validate the dsig:Signature.
Well, what do I am doing? A: I am doing a webservice that receives XML as parameter. For example:
ProcessLoteRps(XmlDocument loteRpsXml);
When I receive the Xml (that contais the elements LoteRps and Signature), I need validate the XML, to verify if the LoteRps and Signature are in the right sctruture. I don't have problem when verifying the LoteRps structure, because I can add the schema, but, when I add the schema for Signature, then I get the error. The code to validate:
//function that returns the erros of the XML structure - if is wrong - in a list of string
public static List<string> ValidaXMLComXSD(XmlDocument xmlTemp)
{
List <string> lista_erros = new List<string>();
try
{
// add the schemas...
xmlTemp.Schemas.Add("http://www.abrasf.org.br/ABRASF/arquivos/nfse.xsd", "Models\XSD\NFSE.xsd");
// HEY BRO, THE PROBLEM OCCURS HERE
xmlTemp.Schemas.Add("http://www.w3.org/2000/09/xmldsig#", HttpContext.Current.Server.MapPath("~/") + @"Models\XSD\xmldsig-core-schema20020212.xsd");
}
catch (Exception e)
{
throw new Exception("Erro ao validar o XML: " + e.Message);
}
}
As we can see, I am adding 2 schemas:
- To validate the RPS (via XSD);
- To validate the Signature (via DTD).
If I do not add the schema for Signature, I get the following error:
The 'http://www.w3.org/2000/09/xmldsig#:Signature' element is not declared.
Ok, then, theoretically I have to add the Signature schema to validate as we can see below:
xmlTemp.Schemas.Add("http://www.w3.org/2000/09/xmldsig#", HttpContext.Current.Server.MapPath("~/") + @"Models\XSD\xmldsig-core-schema20020212.xsd");
But, when I add that schema, I get another (known) error:
For security reasons DTD is prohibited in this XML document. To enable DTD processing set the ProhibitDtd property on XmlReaderSettings to false and pass the settings into XmlReader.Create method.
Well, researching at google, i found a lot of links:
Problem validation a XML file with a local DTD file in C#
http://www.eggheadcafe.com/microsoft/Csharp/33292915/xml-prohibitdtd-error.aspx
I tried a lot of stuffs, but, still getting error..
Then I tried to invent:
public static List ValidaXMLComXSD(XmlDocument xmlTemp) {
List <string> lista_erros = new List<string>();
try
{
#region Test
XmlErrors xmlErros = new XmlErrors();
XmlReaderSettings xsdSettings = new XmlReaderSettings();
xsdSettings.ValidationType = ValidationType.Schema;
xsdSettings.ValidationEventHandler += new ValidationEventHandler(xmlErros.XmlErrorsHandler);
XmlReaderSettings dtdSettings = new XmlReaderSettings();
dtdSettings.ValidationType = ValidationType.DTD;
dtdSettings.ProhibitDtd = false;
dtdSettings.ValidationEventHandler += new ValidationEventHandler(xmlErros.XmlErrorsHandler);
xmlTemp.Schemas.Add(null, XmlReader.Create(NFSE_XSD_PATH, xsdSettings));
// The error occurs below
xmlTemp.Schemas.Add(null, XmlReader.Create(NFSE_XSD_SIG_PATH, dtdSettings));
And a new error appears:
An error has occurred while opening external DTD 'http://www.w3.org/2001/XMLSchema.dtd': The server committed a protocol violation. Section=ResponseStatusLine
Then, that broken my leggs...
I don't know what to do... :s
Please, I need your help.
Best regards, Dan