C# 使用类似的标记(元素)名称更新xml innerText
我的问题是,我的xml文件中有一个最多出现3次的电子邮件元素,如下所示:C# 使用类似的标记(元素)名称更新xml innerText,c#,xml,innertext,C#,Xml,Innertext,我的问题是,我的xml文件中有一个最多出现3次的电子邮件元素,如下所示: <contact> <email>A</email> <email>B</email> <email>C</email> </contact> 只有第一封电子邮件被编辑,由于相同的xml元素名称,因此存在覆盖问题 <contact> <email>F</email> <e
<contact>
<email>A</email>
<email>B</email>
<email>C</email>
</contact>
只有第一封电子邮件被编辑,由于相同的xml元素名称,因此存在覆盖问题
<contact>
<email>F</email>
<email>B</email>
<email>C</email>
</contact>
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>
profilepic/default.jpg
亨德
卡西欧
女性
1985-04-23
4453278
3451390
54356635
卡西欧。hender@mail.com
heder@aol.com
hencasio@yahoo.com
问题
即使当您从XPath查询中获得在XmlNodeList
中返回的多个联系人时,您也只是在更新第一个节点
如果您确定XPath查询应该返回一个联系人或不返回联系人,那么应该使用
这是
更新:(解决方案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";
谢谢你的回复。抱歉,哈桑·尼萨,我可能对我的代码不够清楚。我现在已经编辑了原文。如何在XmlNodeList中再次搜索?是否可能?XmlNodeList表示节点的有序集合。可以使用循环遍历XmlNodeList。如果您确定将返回一个结果,那么可以使用
SelectSingleNode
。我已经尝试过了,但是XmlNodeList似乎有问题,它实际上没有在从XPath查询获得的XmlNode中搜索。其他值例如:photo、firstname等。。正在更新到XPath搜索的XmlNode。但只有电子邮件在它之外更新;在第一个联系人元素标记中。电子邮件节点发生了什么?还是只有第一个节点得到更新?你能更新你的问题吗?我已经通过添加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>
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";
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";