0

I'm doing a basic exercise in C# that uses a XML file as a database. The program must allow the user to search, edit, delete, and add a person's registration.

The search method is working, but now I want to implement functionality to find the person regardless of whether uppercase/lowercase letters are used. For example, as I have it now, if I search "MATHEUS" and the database (XML file) has "matheus", my program does not seem to find that person.

public static List<Entidades.Pessoa> Listar(string nome, string cpfcnpj)
    {
        //Variável de retorno
        List<Entidades.Pessoa> pessoas = new List<Entidades.Pessoa>();

        //Carrega o arquivo xml 
        XmlDocument xmlDocument = new XmlDocument();
        xmlDocument.Load("Database.xml");

        //Representa uma coleção ordenada de nós.
        XmlNodeList xmlNodeListPessoa = null;

        if ((cpfcnpj != null) && (cpfcnpj != ""))
        {
            //Cria uma lista somente com a identificação informada pelo cpfcnpj
            xmlNodeListPessoa = xmlDocument.SelectNodes(string.Format("Lista/Pessoa[CpfCnpj='{0}']", cpfcnpj));
        }

        else if ((nome != null) && (nome != ""))
        {
            //Cria uma lista somente com a identificação informada pelo nome
            xmlNodeListPessoa = xmlDocument.SelectNodes(string.Format("Lista/Pessoa[contains(Nome,'{0}')]", nome));
            //xmlNodeListPessoa = xmlDocument.SelectNodes(string.Format("Lista/Pessoa[starts-with(Nome,'{0}')]", nome));
        }

        else
        {
            //Senao criar uma lista normal 
            xmlNodeListPessoa = xmlDocument.SelectNodes(string.Format("Lista/Pessoa"));
        }

        //Carrega
        foreach (XmlElement xmlElementPessoa in xmlNodeListPessoa)
        {
            //incializar a instancia do objeto pessoa
            Entidades.Pessoa objPessoa = Entidade(xmlElementPessoa);

            // adicionar objeto ba lista
            pessoas.Add(objPessoa);
        }

        //Retorna
        return pessoas;

    }
quetzalcoatl
  • 32,194
  • 8
  • 68
  • 107
  • Try and write the code and, if you hit a specific problem, *then* post a question, including the relevant code that you are having trouble getting working. – Ant P Jan 08 '15 at 18:57
  • 1
    yeah show us the search function code – prospector Jan 08 '15 at 18:59
  • 1
    You need to post your code because I don't know how you are searching. But to find a string and ignore case, you can do something like this: `xml.IndexOf("MATHEUS", StringComparison.OrdinalIgnoreCase)` – Icemanind Jan 08 '15 at 19:00
  • possible duplicate of [case-insensitive matching in xpath?](http://stackoverflow.com/questions/2893551/case-insensitive-matching-in-xpath) – quetzalcoatl Jan 08 '15 at 19:07

1 Answers1

0

I've very close duplicate, so please see the answer there and think it over. It does not match perfectly your question, since the XPath query there is = and you use contains, but the idea is the same: convert everything to lowercase before searching.

Try changing:

Lista/Pessoa[contains(Nome,'{0}')]  <- nome

to

Lista/Pessoa[contains(lower-case(Nome),'{0}')]  <- nome.ToLowerCase()

and be sure to pass nome.ToLowerCase() instead of just nome. Both sides of matching must be "normalized"!

quetzalcoatl
  • 32,194
  • 8
  • 68
  • 107