Import XML File Data to Array

Asked

Viewed 195 times

0

Good morning, I have the following XML code.

<?xml version="1.0" encoding="utf-8"?>
<Regras>
  <Regra Id="1" ExtensaoArquivo=".RET" PastaOrigem="C:\Nova pasta\" PastaDestino="C:\Nova pasta (4)\">
    <depara IdRegra="1" Codigo="1" De="FORN" Para="Fornecedor"/>
    <depara IdRegra="1" Codigo="2" De="3XKP" Para="AEFL"/>
    <depara IdRegra="1" Codigo="3" De="MOV" Para="Retorno"/>
  </Regra>
  <Regra Id="2" ExtensaoArquivo=".RET" PastaOrigem="C:\Nova pasta\" PastaDestino="C:\Nova pasta (2)\">
    <depara IdRegra="2" Codigo="1" De="FORN" Para="Fornecedor"/>
    <depara IdRegra="2" Codigo="2" De="4I8O" Para="Loja"/>
    <depara IdRegra="2" Codigo="3" De="MOV" Para="Retorno"/>
  </Regra>
</Regras>

I would like to upload his data to an array, and I need to bring all the information contained in the xml to the array.

private void BuscarPorArquivosToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LerArquivoXml arquivos = new LerArquivoXml("Teste", "Config.xml");
            XmlDocument xmlDocument = new XmlDocument();
            Regras criterios = new Regras();
            xmlDocument.Load(criterios.GetCaminhoRegras().ToString());
            XmlNode raiz = xmlDocument.SelectSingleNode(@"/Regras");
            //dgvListagemArquivos.Rows.Clear();
            foreach (XmlNode no in raiz.ChildNodes)
            {
                string Id = no.Attributes["Id"].Value;
                string ExtensaoArquivo = no.Attributes["ExtensaoArquivo"].Value;
                string PastaOrigem = no.Attributes["PastaOrigem"].Value;
                string PastaDestino = no.Attributes["PastaDestino"].Value;
            }

            XmlNode filho = xmlDocument.SelectSingleNode(@"/Regras/Regra");
            var ele = System.Xml.Linq.XElement.Load(criterios.GetCaminhoRegras().ToString());
            int cont = filho.SelectNodes(@"/Regras/Regra/depara").Count;

            string[,] depara = new string[cont*4, 4];

            for (int l = 0; l < cont*cont;)
            {
                foreach (XmlNode child in filho.ChildNodes)
                {
                    string IdRegra = child.Attributes["IdRegra"].Value;
                    string Codigo = child.Attributes["Codigo"].Value;
                    string De = child.Attributes["De"].Value;
                    string Para = child.Attributes["Para"].Value;

                    for (int c = 0; c < 4; c++)
                    {
                        depara[l, c] = IdRegra.ToString();
                        depara[l, c + 1] = Codigo.ToString();
                        depara[l, c + 2] = De.ToString();
                        depara[l, c + 3] = Para.ToString();
                        c = 4;
                        l += 1;


                    }
                }
            }

        }
  • Has to be array? it would not be easier to play on a list objects that has the properties of the xml?

  • And what is the error presented?

  • I am not able to make the foreach go through all objects of the tags stumbles. It only searches the data of the first tag stumbles.

  • @Barbetta could be yes.

1 answer

0

Probably your code would work if the XML had no statement(<?xml version="1.0" encoding="utf-8"?>)

In such cases it is necessary to "skip" the statement to catch the nodes.

A class of Regra and a DePara, where Regra has a list of DePara

public class Regra
{
    public int Id { get; set; }
    public string ExtensaoArquivo { get; set; }
    public string PastaOrigem { get; set; }
    public string PastaDestino { get; set; }

    public List<DePara> DesParas { get; set; } = new List<DePara>();
}

public class DePara
{
    public int IdRegra { get; set; }
    public int Codigo { get; set; }
    public string De { get; set; }
    public string Para { get; set; }
}

So we carry the XML and we do the Load(), after that we selected the nodes with tag "Rule", which will return a list with two items.

In the foreach we check whether the node is null and, if so, that node is jumped. We still catch the nodes children of node ""and finally we set up the object.

XmlTextReader xmlReader = new XmlTextReader(@"caminho_arquivo");
XmlDocument doc = new XmlDocument();
doc.Load(xmlReader);
var nodes = doc.DocumentElement.SelectNodes("Regra");
List<Regra> regras = new List<Regra>();
Regra regra;
foreach (XmlNode chldNode in nodes)
{
    if (chldNode.Attributes == null) continue;
    regra = new Regra
    {
        Id = Convert.ToInt32(chldNode.Attributes["Id"].Value),
        ExtensaoArquivo = chldNode.Attributes["ExtensaoArquivo"].Value,
        PastaOrigem = chldNode.Attributes["PastaOrigem"].Value,
        PastaDestino = chldNode.Attributes["PastaDestino"].Value
    };
    foreach (XmlNode child in chldNode.ChildNodes)
    {
        if (child.Attributes == null) continue;

        regra.DesParas.Add(new DePara
        {
            IdRegra = Convert.ToInt32(child.Attributes["IdRegra"].Value),
            Codigo = Convert.ToInt32(child.Attributes["Codigo"].Value),
            De = child.Attributes["De"].Value,
            Para = child.Attributes["Para"].Value
        });
    }
    regras.Add(regra);
}

Browser other questions tagged

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