Olá!
Este procedimento abaixo retorna um dataset com os produtos pronto para serem usados na geração da Danfe, ou seja, já retorna no formato correto para preencher o grid de uma Danfe feita por exemplo com o Report Viwer.
Ele recebe o arquivo xml, e varre as tags gerando um Ds com as mesmas.
private DataSet GeraDsProd(string caminhoXml)
{
DataSet _ds = new DataSet();
DataTable _dt = new DataTable("DetalheNFe");
//Instancia doc com o xml
XmlDocument doc = new XmlDocument();
try
{
//le o xml no caminho correto
doc.Load(caminhoXml);
//le o elemento a ser trabalhado
XmlNodeList No = doc.GetElementsByTagName("prod");
//cria as colunas para serem colocandas no dataset.
_dt.Columns.Add("cProd");
_dt.Columns.Add("xProd");
_dt.Columns.Add("NCM");
_dt.Columns.Add("CFOP");
_dt.Columns.Add("uCom");
_dt.Columns.Add("qCom");
_dt.Columns.Add("vUnCom");
_dt.Columns.Add("vProd");
_dt.Columns.Add("vUnTrib");
_dt.Columns.Add("CST");
_dt.Columns.Add("vBC");
_dt.Columns.Add("pICMS");
_dt.Columns.Add("vICMS");
_dt.Columns.Add("vIPI");
_dt.Columns.Add("pIPI");
//carrega o dataset
_ds.Tables.Add(_dt);
//Leitura dos nos selecionados
foreach (XmlNode book in No)
{
//Criando Registros
DataRow _row;
_row = _dt.NewRow();
_dt.Rows.Add(_row);
for (int i = 0; i < book.ChildNodes.Count; i++)
{
//Verifica o nome do no correspondente a ser colocado na dataset
switch (book.ChildNodes.Item(i).Name)
{
case "cProd":
_row["cProd"] = book.ChildNodes.Item(i).InnerXml;
break;
case "xProd":
_row["xProd"] = book.ChildNodes.Item(i).InnerXml;
break;
case "NCM":
_row["NCM"] = book.ChildNodes.Item(i).InnerXml;
break;
case "CFOP":
_row["CFOP"] = book.ChildNodes.Item(i).InnerXml;
break;
case "uCom":
_row["uCom"] = book.ChildNodes.Item(i).InnerXml;
break;
case "qCom":
_row["qCom"] = string.Format("{0:d}", (book.ChildNodes.Item(i).InnerXml));
break;
case "vUnCom":
_row["vUnCom"] = string.Format("{0:d}", book.ChildNodes.Item(i).InnerXml);
break;
case "vProd":
_row["vProd"] = string.Format("{0:d}", (book.ChildNodes.Item(i).InnerXml));
break;
case "vUnTrib":
_row["vUnTrib"] = string.Format("{0:d}", (book.ChildNodes.Item(i).InnerXml));
break;
}
}
_row["vBC"] = "0.00";
_row["pICMS"] = "0.00";
_row["vICMS"] = "0.00";
_row["vIPI"] = "0.00";
_row["pIPI"] = "0.00";
if (book.NextSibling.Name.ToString() == "imposto")
{
for (int i = 0; i < book.NextSibling.ChildNodes.Count; i++)
{
switch (book.NextSibling.ChildNodes.Item(i).Name.ToString())
{
case "ICMS":
//verifica quantos filhos de icms estao dentro do pai
for (int ii = 0; ii < book.NextSibling.ChildNodes.Item(i).ChildNodes.Count; ii++)
{
for (int iii = 0; iii < book.NextSibling.ChildNodes.Item(i).ChildNodes.Item(ii).ChildNodes.Count; iii++)
{
switch (book.NextSibling.ChildNodes.Item(i).ChildNodes.Item(ii).ChildNodes.Item(iii).Name.ToString())
{
case "CSOSN":
//if (_dsAux.Tables[0].Columns.Contains("CST"))
//{
_row["CST"] = String.Format("{0:000}", Convert.ToInt32(book.NextSibling.ChildNodes.Item(i).ChildNodes.Item(ii).ChildNodes.Item(iii).InnerXml));
//}
break;
case "CST":
//if (_dsAux.Tables[0].Columns.Contains("CST"))
//{
_row["CST"] = String.Format("{0:000}", Convert.ToInt32(book.NextSibling.ChildNodes.Item(i).ChildNodes.Item(ii).ChildNodes.Item(iii).InnerXml));
//}
break;
case "vBC":
if (book.NextSibling.ChildNodes.Item(i).ChildNodes.Item(ii).ChildNodes.Item(iii).InnerXml != string.Empty)
{
_row["vBC"] = book.NextSibling.ChildNodes.Item(i).ChildNodes.Item(ii).ChildNodes.Item(iii).InnerXml;
}
break;
case "pICMS":
if (book.NextSibling.ChildNodes.Item(i).ChildNodes.Item(ii).ChildNodes.Item(iii).InnerXml != string.Empty)
{
_row["pICMS"] = book.NextSibling.ChildNodes.Item(i).ChildNodes.Item(ii).ChildNodes.Item(iii).InnerXml;
}
break;
case "vICMS":
if (book.NextSibling.ChildNodes.Item(i).ChildNodes.Item(ii).ChildNodes.Item(iii).InnerXml != string.Empty)
{
_row["vICMS"] = book.NextSibling.ChildNodes.Item(i).ChildNodes.Item(ii).ChildNodes.Item(iii).InnerXml;
}
break;
}
}
}
break;
case "IPI":
//verifica quantos filhos de icms estao dentro do pai
for (int ii = 0; ii < book.NextSibling.ChildNodes.Item(i).ChildNodes.Count; ii++)
{
for (int iii = 0; iii < book.NextSibling.ChildNodes.Item(i).ChildNodes.Item(ii).ChildNodes.Count; iii++)
{
switch (book.NextSibling.ChildNodes.Item(i).ChildNodes.Item(ii).ChildNodes.Item(iii).Name.ToString())
{
case "vIPI":
if (book.NextSibling.ChildNodes.Item(i).ChildNodes.Item(ii).ChildNodes.Item(iii).InnerXml != string.Empty)
{
_row["vIPI"] = book.NextSibling.ChildNodes.Item(i).ChildNodes.Item(ii).ChildNodes.Item(iii).InnerXml;
}
break;
case "pIPI":
if (book.NextSibling.ChildNodes.Item(i).ChildNodes.Item(ii).ChildNodes.Item(iii).InnerXml != string.Empty)
{
_row["pIPI"] = book.NextSibling.ChildNodes.Item(i).ChildNodes.Item(ii).ChildNodes.Item(iii).InnerXml;
}
break;
}
}
}
break;
}
}//for imposto
}//if imposto
}
return _ds;
}
catch (Exception ex)
{
throw ex;
}
}
Grande Abraço a Todos
Fernando Valota
Grande código... quebrou um puta galhou meu aqui.
ResponderExcluirValeu amigo.
Abraço