我的XML文件格式如下
<AllRecords Rec="查看全部">
<Item Category="按客户查看">
<CustomerName>A </CustomerName>
<CustomerName>B </CustomerName>
<CustomerName>C </CustomerName>
<CustomerName>D </CustomerName>
</Item>
<Item Category="按时间查看">
<CustomerName>1 </CustomerName>
</Item>
</AllRecords>
我现在想删除 <CustomerName>D </CustomerName>这行,该如何操作?谢谢!!请具体点,马上送分,谢谢!
<AllRecords Rec="查看全部">
<Item Category="按客户查看">
<CustomerName>A </CustomerName>
<CustomerName>B </CustomerName>
<CustomerName>C </CustomerName>
<CustomerName>D </CustomerName>
</Item>
<Item Category="按时间查看">
<CustomerName>1 </CustomerName>
</Item>
</AllRecords>
我现在想删除 <CustomerName>D </CustomerName>这行,该如何操作?谢谢!!请具体点,马上送分,谢谢!
XmlNode parent = xmlDoc.SelectSingleNode("Item[@Category='按客户查看']");
XmlNode subNode = parent.SelectSingleNode("CustomerName[text() = 'D']");
parent.RemoveChild(subNode);
1楼的xmlpath可以实现
document.Load(xmlPath);
string strFind = String.Format("//AllRecords/Item[@Category=\"{0}\"]", "按客户查看");
System.Xml.XmlNodeList nodeList = document.SelectNodes(strFind);
foreach (System.Xml.XmlNode node in nodeList.ChildNodes)
{
System.Xml.XmlElement xep = (System.Xml.XmlElement)node;
if (xep.InnerText== "D")
{
nodeList.RemoveChild(node);
break;
}
}
document.Save(xmlPath);
没有测试,你自己测试一下
XmlNode parent = xmlDoc.SelectSingleNode(strFind);就可以了,完整程序为:...
private XmlDocument xmlDoc;
private void LoadXml()
{
xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("tree.xml"));
}
LoadXml();
string strFind = String.Format("//AllRecords/Item[@Category=\"{0}\"]", "按客户查看");
string strCustomerName = ((Label)(GridView1.Rows[e.RowIndex].Cells[1].Controls[1])).Text.ToString();//此处选出gridview所选行文本
XmlNode parent = xmlDoc.SelectSingleNode(strFind);
XmlNode subNode = parent.SelectSingleNode("CustomerName[text() ='"+strCustomerName+"']");
parent.RemoveChild(subNode);
xmlDoc.Save(Server.MapPath("tree.xml"));
...