Beispiel #1
0
        private void ValidarArquivo()
        {
            string xml2txt  = "";
            string hashInfo = "";

            using (Stream stream = FileUpload1.PostedFile.InputStream)
            {
                TxtMsg.Value += $"Arquivo: {FileUpload1.PostedFile.FileName}{Environment.NewLine}";

                using (StreamReader reader = new StreamReader(stream, Encoding.Default))
                {
                    string fileContents = reader.ReadToEnd();

                    //ler a string em um objeto xmldocument
                    var xmlDoc = new XmlDocument();
                    xmlDoc.LoadXml(fileContents);
                    var nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
                    nsmgr.AddNamespace("", "http://www.ans.gov.br/padroes/tiss/schemas");

                    //pegar a versão do tiss
                    XmlNode versaoNode = xmlDoc.SelectSingleNode("//*[local-name()='padrao']", nsmgr);
                    if (versaoNode == null)
                    {
                        versaoNode = xmlDoc.SelectSingleNode("//*[local-name()='versaoPadrao']", nsmgr);
                    }

                    //validar o arquivo com os schemas
                    if (ValidarVersaoTiss(versaoNode.InnerText))
                    {
                        stream.Position = 0;
                        ValidadorSchema.Validar(stream, Server.MapPath(string.Format($"schemas\\tiss_v{versaoNode.InnerText}\\tissV3_02_00.xsd")));
                    }

                    _count          = 0;
                    _nodeIndex      = 1;
                    _nodeTotalCount = xmlDoc.SelectNodes("descendant::*").Count;
                    XmlNode nodeList = xmlDoc.SelectSingleNode("//*[local-name()='cabecalho']/*", nsmgr)?.ParentNode?.ParentNode;
                    foreach (XmlNode root in nodeList)
                    {
                        switch (root.LocalName.ToString())
                        {
                        case "cabecalho":
                            _nodeIndex += root.SelectNodes("descendant::*").Count;
                            AtualizarProgresso((int)Math.Floor((double)_nodeIndex / _nodeTotalCount * 100));
                            xml2txt += root.InnerText;
                            break;

                        case "epilogo":
                            _nodeIndex += root.SelectNodes("descendant::*").Count;
                            AtualizarProgresso((int)Math.Floor((double)_nodeIndex / _nodeTotalCount * 100));
                            hashInfo = root.InnerText?.ToUpper();
                            break;

                        case "hash":
                            _nodeIndex += root.SelectNodes("descendant::*").Count;
                            AtualizarProgresso((int)Math.Floor((double)_nodeIndex / _nodeTotalCount * 100));
                            hashInfo = root.InnerText?.ToUpper();
                            break;

                        default:
                            xml2txt += root.InnerText;

                            //validar se os valores dos textos contém caracteres especiais não válidos no xml
                            ReadXML(root);
                            break;
                        }
                    }
                }
            }

            //calcular o hash
            var hashCalc = Utils.CalculateMD5Hash(xml2txt)?.ToUpper();

            TxtMD5.Value = hashCalc;

            //comparar o hash enviado com o calculado
            if (hashInfo != hashCalc)
            {
                TxtMsg.Value += $"HASH INVÁLIDO{Environment.NewLine}INFORMADO: {hashInfo}{Environment.NewLine}CALCULADO: {hashCalc}";
            }
            else
            {
                TxtMsg.Value += "HASH OK!!!";
            }
        }
Beispiel #2
0
        private void ValidarArquivo()
        {
            string xml2txt  = "";
            string hashInfo = "";

            using (Stream stream = FileUpload1.PostedFile.InputStream)
            {
                TxtMsg.Value += $"Arquivo: {FileUpload1.PostedFile.FileName}{Environment.NewLine}";

                using (StreamReader reader = new StreamReader(stream, Encoding.Default))
                {
                    string fileContents = reader.ReadToEnd();

                    //ler a string em um objeto xmldocument
                    var xmlDoc = new XmlDocument();
                    xmlDoc.LoadXml(fileContents);

                    //validar o arquivo com os schemas
                    stream.Position = 0;
                    ValidadorSchema.Validar(stream, Server.MapPath(string.Format($"Schemas\\sib.xsd")));

                    _count          = 0;
                    _nodeIndex      = 1;
                    _nodeTotalCount = xmlDoc.SelectNodes("descendant::*").Count;
                    var nodeList = xmlDoc.SelectNodes("//mensagemSIB/*");
                    foreach (XmlNode root in nodeList)
                    {
                        switch (root.Name.ToString())
                        {
                        case "cabecalho":
                            _nodeIndex += root.SelectNodes("descendant::*").Count;
                            AtualizarProgresso((int)Math.Floor((double)_nodeIndex / _nodeTotalCount * 100));
                            xml2txt += root.InnerText;
                            break;

                        case "epilogo":
                            _nodeIndex += root.SelectNodes("descendant::*").Count;
                            AtualizarProgresso((int)Math.Floor((double)_nodeIndex / _nodeTotalCount * 100));
                            hashInfo = root.InnerText?.ToUpper();
                            break;

                        case "hash":
                            _nodeIndex += root.SelectNodes("descendant::*").Count;
                            AtualizarProgresso((int)Math.Floor((double)_nodeIndex / _nodeTotalCount * 100));
                            hashInfo = root.InnerText?.ToUpper();
                            break;

                        default:
                            xml2txt += root.InnerText;

                            //validar se os valores dos textos contém caracteres especiais não válidos no xml
                            ReadXML(root);
                            break;
                        }
                    }
                }
            }

            //calcular o hash
            var hashCalc = Utils.CalculateMD5Hash(xml2txt)?.ToUpper();

            TxtMD5.Value = hashCalc;

            //comparar o hash enviado com o calculado
            if (hashInfo != hashCalc)
            {
                TxtMsg.Value += $"HASH INVÁLIDO{Environment.NewLine}INFORMADO: {hashInfo}{Environment.NewLine}CALCULADO: {hashCalc}";
            }
            else
            {
                TxtMsg.Value += "HASH OK!!!";
            }
        }