Domanda

My issue is that i have an email element of max 3 occurence in my xml file as shown below:

<contact> 
<email>A</email> 
<email>B</email> 
<email>C</email> 
</contact> 

When i tried to edit the 3 tags with the following code below,

xnl[0]["email"].InnerText = "D"; 
xnl[0]["email"].InnerText = "E"; 
xnl[0]["email"].InnerText = "F"; 

Only the first email is edited, there is the problem of overwriting because of same xml element name.

<contact> 
<email>F</email> 
<email>B</email> 
<email>C</email> 
</contact> 

I tried xnl[0]["email"][0].InnerText = "D"; for choosing the first email element name, But this did not work. Any suggestion?

UPDATE: (with code)

c# code:

        public String updateContact(Int32 getPhoneNumber, Int32 getWorkNumber, Int32 getMobileNumber, String photo, String firstName, String lastName, String gender, String dateOfBirth, Int32 home, Int32 work, Int32 mobile, String email1, String email2, String email3)
    {
        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::* ");

        if (xnl.Count != 0)
        {
            xnl[0]["photo"].InnerText = photo;
            xnl[0]["firstName"].InnerText = firstName;
            xnl[0]["lastName"].InnerText = lastName;
            xnl[0]["gender"].InnerText = gender;
            xnl[0]["dateOfBirth"].InnerText = dateOfBirth;
            xnl[0]["phone"]["home"].InnerText = home.ToString();
            xnl[0]["phone"]["work"].InnerText = work.ToString();
            xnl[0]["phone"]["mobile"].InnerText = mobile.ToString();
            xnl[0]["email"].InnerText = email1;
            xnl[0]["email"].InnerText = email2;
            xnl[0]["email"].InnerText = email3;

        }

        doc.Save("xmlFile/xml/myContactBook/src/myContactBook.xml");

        return "Updated";
    }

xml code:

<contact>
<photo>profilepic/default.jpg</photo>
<firstName>Hender</firstName>
<lastName>Casio</lastName>
<gender>Female</gender>
<dateOfBirth>1985-04-23</dateOfBirth>
<phone>
  <home>4453278</home>
  <work>3451390</work>
  <mobile>54356635</mobile>
</phone>
<email>casio.hender@mail.com</email>
<email>heder@aol.com</email>
<email>hencasio@yahoo.com</email>
</contact>
È stato utile?

Soluzione

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";
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top