Problem
Even when you are getting multiple contacts returned in your XmlNodeList
from your XPath query, you are just updating first node.
If you are sure that your XPath query should return one or no contact then you should use SelectSingleNode.
Here is fiddle.
XmlDocument doc = new XmlDocument();
doc.Load("xmlFile/xml/myContactBook/src/myContactBook.xml");
XmlNode xn = doc.SelectSingleNode("/contactBook/contact/phone[home='" + getPhoneNumber + "']/parent::* | /contactBook/contact/phone[work='" + getWorkNumber + "']/parent::* | /contactBook/contact/phone[mobile='" + getMobileNumber + "']/parent::* ");
if (xn != null)
{
xn["photo"].InnerText = photo;
xn["firstName"].InnerText = firstName;
xn["lastName"].InnerText = lastName;
xn["gender"].InnerText = gender;
xn["dateOfBirth"].InnerText = dateOfBirth;
xn["phone"]["home"].InnerText = home.ToString();
xn["phone"]["work"].InnerText = work.ToString();
xn["phone"]["mobile"].InnerText = mobile.ToString();
XmlNodeList xnl = xn.SelectNodes("email");
xnl[0].InnerText = email1;
xnl[1].InnerText = email2;
xnl[2].InnerText = email3;
}
doc.Save("xmlFile/xml/myContactBook/src/myContactBook.xml");
return "Updated";
UPDATE: (Solution 2) In case multiple results are returned from your XPath query:
XmlDocument doc = new XmlDocument();
doc.Load("xmlFile/xml/myContactBook/src/myContactBook.xml");
XmlNodeList xnl = doc.SelectNodes("/contactBook/contact/phone[home='" + getPhoneNumber + "']/parent::* | /contactBook/contact/phone[work='" + getWorkNumber + "']/parent::* | /contactBook/contact/phone[mobile='" + getMobileNumber + "']/parent::* ");
foreach(XmlNode xn in xnl)
{
xn["photo"].InnerText = photo;
xn["firstName"].InnerText = firstName;
xn["lastName"].InnerText = lastName;
xn["gender"].InnerText = gender;
xn["dateOfBirth"].InnerText = dateOfBirth;
xn["phone"]["home"].InnerText = home.ToString();
xn["phone"]["work"].InnerText = work.ToString();
xn["phone"]["mobile"].InnerText = mobile.ToString();
XmlNodeList xnlElement = xn.SelectNodes("email");
xnlElement[0].InnerText = email1;
xnlElement[1].InnerText = email2;
xnlElement[2].InnerText = email3;
}
doc.Save("xmlFile/xml/myContactBook/src/myContactBook.xml");
return "Updated";