私の問題は、以下に示すように、xml ファイルに最大 3 回出現する電子メール要素があることです。
<contact>
<email>A</email>
<email>B</email>
<email>C</email>
</contact>
以下のコードで3つのタグを編集しようとしたところ、
xnl[0]["email"].InnerText = "D";
xnl[0]["email"].InnerText = "E";
xnl[0]["email"].InnerText = "F";
最初のメールのみを編集すると、xml 要素名が同じであるために上書きされる問題があります。
<contact>
<email>F</email>
<email>B</email>
<email>C</email>
</contact>
最初のメール要素名を選択しようとxnl[0]["email"][0].InnerText = "D";しましたが、うまくいきませんでした。なにか提案を?
更新:(コード付き)
c# コード:
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 コード:
<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>