Import XML file data into Array

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 load its data into 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;


                    }
                }
            }

        }
    
asked by anonymous 21.05.2018 / 15:18

1 answer

0

Your code would probably work if XML had no declaration ( <?xml version="1.0" encoding="utf-8"?> )

In these cases you need to "skip" the statement to get the nodes .

For example, a class of Regra and DePara was created, 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 load XML and make Load() , after that we select the nodes with tag "Rule", which will return a list with two items. >

No foreach checks to see if node is null and, if so, node is skipped. We still get the nodes children of node "main" and finally we mount 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);
}
    
22.05.2018 / 04:18