1

I'm reading an XML file and I ask the user to enter the name of the airline you want to search, by leaving the XML in upper case the condition passes and displays the information I want, but when leaving the information in minuscule and minuscule or uppercase, It returns nothing.

public class XMLReader
    {
        public XMLReader()
        {
        }
        //Fin de metdo leerXML.
        public void leerXML()
        {
            Console.WriteLine("Enter the airline you wish to search: ");
            string name;
            name = Console.ReadLine().ToUpper();

            if (!String.IsNullOrEmpty(name))
            {
                XElement info = XElement.Load(
                  @"C:\Users\thoma\Documents\Visual Studio 2019\Backup Files\data.xml");

                var airlines = info.XPathSelectElements("aerolinea");
                foreach (XElement el in airlines)
                {
                    if (!String.IsNullOrEmpty(el.Element("nombre").Value) && 
                      ((string)el.Element("nombre").Value).IndexOf(name) >= 0)
                    {
                        Console.WriteLine((string)el.Element("origen").Value);
                        Console.WriteLine((string)el.Element("destino").Value);
                        Console.WriteLine((string)el.Element("fecha").Value);
                    }
                }
            }
        }

        static void Main(string[] args)
        {
            XMLReader xmlReader = new XMLReader();
            xmlReader.leerXML();
            Console.ReadLine();
        }
    }

Here is the XML

<?xml version="1.0" encoding="iso-8859-1"?>
<aerolineas>
    <aerolinea id="01">
        <nombre>VIVA COLOMBIA</nombre>
        <origen>BOG</origen>
        <destino>MDE</destino>
        <fecha>01/03/2019</fecha>
    </aerolinea>
    <aerolinea id="02">
        <nombre>HK Express</nombre>
        <origen>BOG</origen>
        <destino>CTG</destino>
        <fecha>01/06/2019</fecha>
    </aerolinea>
    <aerolinea id="03">
        <nombre>Volotea</nombre>
        <origen>PEI</origen>
        <destino>BOG</destino>
        <fecha>01/09/2019</fecha>
    </aerolinea>
    <aerolinea id="04">
        <nombre>Vueling</nombre>
        <origen>MDE</origen>
        <destino>BOG</destino>
        <fecha>01/12/2019</fecha>
    </aerolinea>
</aerolineas>
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179

1 Answers1

1

Change

 name = Console.ReadLine().ToUpper();

to

 name = Console.ReadLine();

Your original code is reading the user input, then changing it to all Uppercase before storing it in the "name" variable.

Now, to really solve your problem I need to point out two things:

1) Your XML <nombre> is Uppercase with VIVA COLOMBIA, but mixed with Volotea. The other <nombre> fields have similar issues.

2) You are dealing with user input, which can vary quite widely depending on who is typing. Some user may type all caps, some all lowercase (you call it minuscule).

The real solve is that you should read everything as one case. So, this stays the same as original: name = Console.ReadLine().ToUpper();

Change if (!String.IsNullOrEmpty(el.Element("nombre").Value.) && ((string)el.Element("nombre").Value.IndexOf(name) >= 0)

to this if (!String.IsNullOrEmpty(el.Element("nombre").Value.ToUpper()) && ((string)el.Element("nombre").Value.ToUpper()).IndexOf(name) >= 0)

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
slightly drifting
  • 302
  • 1
  • 4
  • 16