3
I have this function to validate an xml:
public static bool ValidarXML(string pathXML, string pathSchema, ref string retorno)
{
bool falhou;
// Define o tipo de validação
XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Schema;
// Carrega o arquivo de esquema
XmlSchemaSet schemas = new XmlSchemaSet();
settings.Schemas = schemas;
// Quando carregar o eschema, especificar o namespace que ele valida
// e a localização do arquivo
schemas.Add(null, pathSchema);
// Especifica o tratamento de evento para os erros de validacao
// settings.ValidationEventHandler += ValidationEventHandler;
// cria um leitor para validação
XmlReader validator = XmlReader.Create(pathXML, settings);
falhou = false;
try
{
// Faz a leitura de todos os dados XML
while (validator.Read()) { }
}
catch (XmlException err)
{
// Um erro ocorre se o documento XML inclui caracteres ilegais
// ou tags que não estão aninhadas corretamente
retorno = "Ocorreu um erro critico durante a validação do XML: " + err.Message;
falhou = true;
}
finally
{
validator.Close();
retorno = "Arquivo validado com sucesso";
}
return !falhou;
}
However I am trying to pass the file, and is returning me error:
ValidarXML("E:\\nota.xml", typeof(tcNfse), ref a);
The schema in the case is the typeof(tcNfse)
It returns the following error:
cannot convert from "System.Type" to "string"
As in the comments, I was able to solve this error above by adding the suggestion:
ValidarXML("E:\\nota.xml", nameof(tcNfse), ref a);
But he says he can’t find the tcNfse
, he is looking for the file inside the project, in case I have to pass the location of the file, is it ? instead of it inside the program?
(string pathXML, string pathSchema, ref string retorno)
just look at the signature, where the second parameter needs to be astring
!!! understood the problem?– novic
@Virgilionovic I understood, but do not know how to fix, pass the schema as string.
– Mariana
maybe a
nameOf(tcNfse)
solve, maybe I don’t know how to try !!! likeValidarXML("E:\\nota.xml", nameOf(tcNfse), ref a);
– novic
@Virgilionovic I’m trying to get through but he says he can’t find, Could not find file 'E: ERP Erp tcNfse'. schema is in the project, the xsd file, all right, would it be right to pass the path, and not the
tcNfse
?– Mariana
So I do not know the purpose of the method ... just pointed out the problem reported by the exception generated
– novic
@Virgilionovic, I’m going to do a little more digging, see if I can sort it out, thanks for the time being.
– Mariana
I did it this way:
ValidarXML("E:\\nota.xml", "E:\\xmldsig-core-schema20020212.xsd", ref a);
and I managed to fix it. Here’s a tip for those who have the same problem.– Mariana