0
At the moment where I import XML information, it does not separate and play to the XML_List
, for continuity to the code, it returns no value in that part of the code:
XDocument xDoc = XDocument.Load(xmlPath);
List<Dados_XML> xml_list = xDoc.Descendants("ide").Select(ide =>
new Dados_XML
{
Nome_Cliente = ide.Element("nome_cliente").Value,
nNF = Convert.ToInt32(ide.Element("nNF").Value),
Serie = Convert.ToInt32(ide.Element("Serie").Value)
}).ToList();
That would be the whole code.
public ActionResult Index(HttpPostedFileBase xmlFile)
{
if(xmlFile.ContentType.Equals("application/xml") || xmlFile.ContentType.Equals("text/xml"))
{
try
{
var xmlPath = Server.MapPath("~/Content/" + xmlFile.FileName);
xmlFile.SaveAs(xmlPath);
XDocument xDoc = XDocument.Load(xmlPath);
List<Dados_XML> xml_list = xDoc.Descendants("ide").Select(ide =>
new Dados_XML
{
Nome_Cliente = ide.Element("nome_cliente").Value,
nNF = Convert.ToInt32(ide.Element("nNF").Value),
Serie = Convert.ToInt32(ide.Element("Serie").Value)
}).ToList();
using (XML_Conn xmlCon = new XML_Conn())
{
foreach (var i in xml_list)
{
var v = xmlCon.Dados_XML.Where(a => a.Id.Equals(i.nNF)).FirstOrDefault();
if (v != null)
{
v.Id = i.Id;
v.nNF = i.nNF;
v.Serie = i.Serie;
v.Nome_Cliente = i.Nome_Cliente;
}
else
{
xmlCon.Dados_XML.Add(i);
ViewBag.Result = xmlCon.Dados_XML.ToList();
}
}
}
return View("Index");
}
catch
{
ViewBag.Error = "Não pode ser importado o XML!";
return View("Index");
}
}
else
{
ViewBag.Error = "Não pode ser importado o XML!";
return View("Index");
}
}
XML:
-<ide>
<cUF>35</cUF>
<cNF>98603820</cNF>
<natOp>DEV. PRODUTOS</natOp>
<mod>55</mod>
<serie>2</serie>
<nNF>54341</nNF>
<dhEmi>2019-08-02T17:44:35-03:00</dhEmi>
<dhSaiEnt>2019-08-02T17:44:59-03:00</dhSaiEnt>
<tpNF>0</tpNF>
<idDest>1</idDest>
<cMunFG>3542602</cMunFG>
<tpImp>2</tpImp>
<tpEmis>1</tpEmis>
<cDV>0</cDV>
<tpAmb>1</tpAmb>
<finNFe>4</finNFe>
<indFinal>0</indFinal>
<indPres>9</indPres>
<procEmi>0</procEmi>
<verProc>11.58.00.00</verProc>
put xml in your question!
– novic
@Virgilionovic put the XML.
– Guilherme Padovam
And where do you have a list? There is no node
nome_cliente
... You’ve already debugged your code?– Leandro Angelo
Including the header! is because of it you are having problems
– novic
Okay, so apart from the
nome_cliente
or by changing the header in case one part of the problem will be solved, the other part is the list, in case I put aList
– Guilherme Padovam
The moment he passes by
List
, it returns value 0– Guilherme Padovam