Repeated nodes xml c#

Asked

Viewed 121 times

1

Hi, I needed some help. I have this code:

XmlNodeList xnList = doc.GetElementsByTagName("Line");
foreach (XmlNode xn in xnList)
{
    string salescode = xn["ProductCode"].InnerText;
    if (salescode == code)
    {
        Console.WriteLine(salescode);
    }
    else;
}

And this code shows the following: inserir a descrição da imagem aqui

But when there are repeated numbers, I just want the first one to appear. How can I do this ?

1 answer

2


You can store the values already displayed in a list, and not display what is already in the list.

For example:

List<string> valoresExibidos = new List<string>();

XmlNodeList xnList = doc.GetElementsByTagName("Line");
foreach (XmlNode xn in xnList)
{
    string salescode = xn["ProductCode"].InnerText;
    if (salescode == code)
    {
        if (valoresExibidos.Contains(salescode))
        {
            continue; // isso faz o laço pular para o próximo nó
        }
        else
        {
            valoresExibidos.Add(salesCode);
            Console.WriteLine(salescode);
        }
    }
}
  • THANK YOU RENAN ! It was exactly what I needed ! Hug

  • @DC I noticed that you asked several questions and no answer accepted, so I think it pertinent to let you know that you can always choose an answer as correct for your question. It is a way to show that your problem has been solved with the answer. To do this, just click on ✓ on the left side of the publication.

Browser other questions tagged

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