Repeated Nodes xml c #

1

Hello, 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:

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

    
asked by anonymous 26.04.2017 / 16:44

1 answer

2

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

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);
        }
    }
}
    
26.04.2017 / 17:03