Type 'http://www.portalfiscal.inf.br/nfe:Tnfe' is not declared

Asked

Viewed 635 times

1

Good morning, usually I avoid to the maximum to ask questions here in the forum because I always find many answers that supply my situation.

I am doing an integration with Sefaz, using Ws Nfe 4.00 Authorization, but when I will do the validation of my XML is returned the error:

Type 'http://www.portalfiscal.inf.br/nfe:TNFe' is not declared

I created my project in Asp.Net Core (Web Api) and this is the code I use to validate:

XmlReaderSettings xmlReaderSettings = new XmlReaderSettings();
xmlReaderSettings.ValidationType = ValidationType.Schema;
xmlReaderSettings.Schemas = new XmlSchemaSet();
xmlReaderSettings.XmlResolver = new XmlUrlResolver() { Credentials = CredentialCache.DefaultCredentials };
xmlReaderSettings.Schemas.Add(null, localSchema);
xmlReaderSettings.ValidationEventHandler += new ValidationEventHandler(delegate (object sender, ValidationEventArgs e)
{
     var ex = new Exception(string.Format("Falha ao validar Xml! Linha: {0}, Coluna: {1}, Mensagem: {2}",
     e.Exception.LineNumber, e.Exception.LinePosition, e.Exception.Message));
     ex.Data.Add("statusCode", HttpStatusCode.BadRequest);
     throw ex;
 });

 using (var stringReader = new StringReader(xml.InnerXml))
 {
     var xmlReader = XmlReader.Create(stringReader, xmlReaderSettings);
     while (xmlReader.Read()) { }                    
     xmlReader.Close();

Remembering that:

  • Same code works on different Core projects;
  • The error happens in the following line "var xmlReader = Xmlreader.Create(stringReader, xmlReaderSettings);"
  • All Nfe 4.00 schemas are up to date;
  • I know the error is not in XML because it works smoothly in a version of an old project;
  • I’ve tried some tips from others who asked and it didn’t work;
  • The schemas I use, can be downloaded in the following link: "http://www.nfe.fazenda.gov.br/PORTAL/exibirArquivo.aspx?conteudo=CoNA9VIgZ3E="

Thanks in advance for the help.

  • Hello Leonardo, in which line occurs the exception?

1 answer

0

I had the same problem. I added the xmlReaderSettings the references of note xsd.

You can find the updated list of schemas in the website of Receita Federal.

In my case it’s like this:

XmlReaderSettings Settings = new XmlReaderSettings();
Settings.Schemas.Add("http://www.portalfiscal.inf.br/nfe", XmlReader.Create(assets.Open("Schemas/nfe_v4.00.xsd")));
Settings.Schemas.Add("http://www.portalfiscal.inf.br/nfe", XmlReader.Create(assets.Open("Schemas/leiauteNFe_v4.00.xsd")));
Settings.Schemas.Add("http://www.w3.org/2000/09/xmldsig#", XmlReader.Create(assets.Open("Schemas/xmldsig-core-schema_v1.01.xsd")));
Settings.Schemas.Add("http://www.portalfiscal.inf.br/nfe", XmlReader.Create(assets.Open("Schemas/tiposBasico_v4.00.xsd")));
Settings.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);
Settings.ValidationType = ValidationType.Schema;
try
{
    XmlReader validator = XmlReader.Create("/storage/emulated/0/certificados/NotaTeste.xml", Settings)
}
catch (Exception EX)
{
    string ex = EX.ToString();
}

private static void ValidationCallBack(object sender, ValidationEventArgs args)
{
    if (args.Severity == XmlSeverityType.Warning)
        Console.WriteLine("\tWarning: Matching schema not found.  No validation occurred." + args.Message);
    else
        Console.WriteLine("\tValidation error: " + args.Message);
    }
} 

Browser other questions tagged

You are not signed in. Login or sign up in order to post.