Find string in XML file

inkedGFX

Well-known member
Joined
Feb 2, 2013
Messages
142
Programming Experience
Beginner
I wrote a method to find a Contact name in an XML file. the method works fine if there are only 2 names in the xml, anymore than 2 and the method does not seem to work. please take a look at the code and let me know if this is the correct way to do this.

  public bool FindContact(string contactName)
        {
            Boolean found = false;
            contactFound.Clear();
            if (!File.Exists(AppDomain.CurrentDomain.BaseDirectory + "\\Assets\\XML\\Contacts.xml"))
            {
                found = false;
            }
            else
            {
                XmlDocument xDoc = new XmlDocument();
                xDoc.Load(AppDomain.CurrentDomain.BaseDirectory + "\\Assets\\XML\\Contacts.xml");


                XmlNodeList nodeList = xDoc.SelectNodes("Contacts/Contact");


                foreach (XmlNode xNode in nodeList)
                {
                    string checkName = xNode["ContactName"].InnerText.ToLower();
                    
                    if (checkName.Trim().ToLower().Contains(contactName.Trim().ToLower()))
                    {
                        //System.Windows.MessageBox.Show(checkName.ToLower());


                        string id = xNode["ID"].InnerText;
                        string regName = xNode["ContactName"].InnerText;
                        string phone = xNode["Phone"].InnerText;
                        string email = xNode["Email"].InnerText;
                        string birthday = xNode["Birthday"].InnerText;
                        string address = xNode["Address"].InnerText;
                        string city = xNode["City"].InnerText;
                        string state = xNode["State"].InnerText;
                        string zip = xNode["Zip"].InnerText;
                        string imageUrl = xNode["ImageUrl"].InnerText;


                        ContactProperty.Name = regName;
                        ContactProperty.ID = id;
                        ContactProperty.Phone = phone;
                        ContactProperty.Email = email;
                        ContactProperty.Birthday = birthday;
                        ContactProperty.Address = address;
                        ContactProperty.City = city;
                        ContactProperty.State = state;
                        ContactProperty.Zip = zip;
                        ContactProperty.ImageUrl = imageUrl;


                        contactFound.Add(checkName);


                        found = true;
                    }
                    else
                    {
                        found = false;
                    }
                }
            }


            return found;
        }


thank you for any help
-InkedGFX
 
Back
Top Bottom