I'm having a small problem importing some XML due to Encode ISO-8859-1. The process is currently divided into two phases, the part that validates the XML and displays a list of items not registered in the system and a list of products that already exist in the system, and the part that actually imports the items and makes associations within them. of the system.
In the first part, when there is an item that does not have an association in the system, it forces the client to associate the XML product with a product in the system before proceeding.
As the product is in XML: "GPM 25 / VALVULA GPMBRASIL PN 16 COMERCIAL 45º 2.1/2 11 BSP X 5 FPP LATAO C/ VOLANTE Ø 90 MM VERMELHO"
How the product looks after being inserted into the system: "GPM 25 / VALVULA GPMBRASIL PN 16 COMERCIAL 45º 2.1/2 11 BSP X 5 FPP LATAO C/ VOLANTE � 90 MM VERMELHO"
I'm capturing the value through my controller like this:
XDocument xdoc = null;
using (StreamReader oReader = new StreamReader(file.InputStream, Encoding.GetEncoding("ISO-8859-1")))
{
xdoc = XDocument.Load(oReader);
}
using (var xmlReader = xdoc.CreateReader())
{
importacao.XmlDocument.Load(xmlReader);
}
Method I use to create the product list:
var produtosDoXML = nodeProdutos.Cast\<XmlNode\>().Select(x =\> new
{
Codigo = x\["prod"\]\["cProd"\].InnerText,
Descricao = x\["prod"\]\["xProd"\].InnerText,
NCM = x\["prod"\]\["NCM"\]?.InnerText,
CEST = x\["prod"\]\["CEST"\]?.InnerText,
EANTributavel = x\["prod"\]\["cEANTrib"\]?.InnerText,
EAN = x\["prod"\]\["cEAN"\]?.InnerText,
UnidadeComercial = x\["prod"\]\["uCom"\]?.InnerText,
Quantidade = Convert.ToDecimal(x\["prod"\]\["qCom"\].InnerText, CultureInfo.InvariantCulture),
Desconto = Convert.ToDecimal(x\["prod"\]\["vDesc"\]?.InnerText, CultureInfo.InvariantCulture),
ValorUnitario = Math.Round(Convert.ToDecimal(x\["prod"\]\["vUnCom"\]?.InnerText, CultureInfo.InvariantCulture), 4),
OrdemDeExibicao = int.Parse(x.Attributes\["nItem"\].Value)
});
foreach (var produtoDoXML in produtosDoXML)
{
var produtos = produtosServicosDosFornecedores.Where(x =\> x.CodigoEquivalente == produtoDoXML.Codigo && x.DescricaoEquivalente == produtoDoXML.Descricao);
if (produtos.Count() > 1)
{
throw new CompraException(CompraException.TypeException.ProdutoEquivalenteDeTerceiroDuplicado, "", produtoDoXML.Codigo);
}
else if (produtos.Count() == 0)
{
var produtosAssociadosAoCodigo = produtosServicosDosFornecedores.Where(x => x.CodigoEquivalente == produtoDoXML.Codigo)
.GroupBy(x => x.ProdutoServicoId);
if (produtosAssociadosAoCodigo.Count() == 1)
{
retornoDaImportacao.AdicionarProdutoNaoAssociadoComCodigoExistente(new RetornoImportacaoXMLDeNFeDeCompraDTO.ProdutoNaoAssociadoComCodigoExistente
{
IdDoProdutoAssociado = produtosAssociadosAoCodigo.Single().First().ProdutoServico.Id,
CodigoDoProdutoAssociado = produtosAssociadosAoCodigo.Single().First().ProdutoServico.Codigo,
DescricaoDoProdutoAssociado = produtosAssociadosAoCodigo.Single().First().ProdutoServico.Descricao,
NomeDoFornecedor = fornecedor.NomeFantasia,
IdDoFornecedor = fornecedor.Id,
Codigo = produtoDoXML.Codigo,
Descricao = produtoDoXML.Descricao,
EAN = produtoDoXML.EAN,
EANTributavel = produtoDoXML.EANTributavel,
CEST = produtoDoXML.CEST,
NCM = produtoDoXML.NCM,
UnidadeComercial = produtoDoXML.UnidadeComercial,
Valor = produtoDoXML.ValorUnitario,
});
}
else
{
retornoDaImportacao.AdicionarErroDeProdutoServico(new RetornoImportacaoXMLDeNFeDeCompraDTO.ErroDeProdutoServico
{
Mensagem = string.Format(Language.Exception_ProdutoXNaoEncontrado, $"[{produtoDoXML.Codigo} - {produtoDoXML.Descricao}]"),
NomeDoFornecedor = fornecedor.NomeFantasia,
IdDoFornecedor = fornecedor.Id,
Codigo = produtoDoXML.Codigo,
Descricao = produtoDoXML.Descricao,
EAN = produtoDoXML.EAN,
EANTributavel = produtoDoXML.EANTributavel,
CEST = produtoDoXML.CEST,
NCM = produtoDoXML.NCM,
UnidadeComercial = produtoDoXML.UnidadeComercial,
Valor = produtoDoXML.ValorUnitario
});
}
}
else
{
retornoDaImportacao.AdicionarProdutoAssociado(new RetornoImportacaoXMLDeNFeDeCompraDTO.ProdutoAssociado
{
IdDoProduto = produtos.First().Id,
CodigoDoProdutoAssociado = produtos.First().ProdutoServico.Codigo,
DescricaoDoProdutoAssociado = produtos.First().ProdutoServico.Descricao,
NomeDoFornecedor = fornecedor.NomeFantasia,
IdDoFornecedor = fornecedor.Id,
Codigo = produtoDoXML.Codigo,
Descricao = produtoDoXML.Descricao,
EAN = produtoDoXML.EAN,
EANTributavel = produtoDoXML.EANTributavel,
CEST = produtoDoXML.CEST,
NCM = produtoDoXML.NCM,
UnidadeComercial = produtoDoXML.UnidadeComercial,
Valor = produtoDoXML.ValorUnitario,
});
}
}
So as the product looks different when it is inserted, when I need to compare it again in the future, it will not find a product that is already registered in the system.
If you are sure that your data is stored in your system (database) with UTF-8 encoding, you can change the XDocument (xdoc) encoding to UTF-8 after it has been read with StreamReader:
so you are sure that your xml file is already UTF-8 encoded when it is saved.