我的问题是我的xml文件中有一个max 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#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代码:
<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>
答案 0 :(得分:1)
<强>问题强>
即使您从XPath查询中获得XmlNodeList
中返回的多个联系人,您也只是更新第一个节点。
如果您确定XPath查询应该返回一个或没有联系人,那么您应该使用SelectSingleNode。
这是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";
更新 :( 解决方案2 )如果您的XPath查询返回了多个结果:
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";