日期:2014-05-18  浏览次数:20644 次

100分求问一XML删除结点问题
</Devices>  
  </Device>
  <UPSInfos>
  <UPSInfo>
  <UpsName>钢笔1</UpsName>
  <UpsStyle>钢笔</UpsStyle>
  <UpsInterface>黑默水</UpsInterface>
  </UPSInfo>
  <UPSInfo>
  <UpsName>钢笔2</UpsName>
  <UpsStyle>钢笔</UpsStyle>
  <UpsInterface>红默水</UpsInterface>
  </UPSInfo>
  </UPSInfos>
  </Device>
</Devices>
我想写用一个函数删去如下,怎么写啊:
  <UPSInfo>
  <UpsName>山特2</UpsName>
  <UpsStyle>山特</UpsStyle>
  <UpsInterface>232</UpsInterface>
  </UPSInfo>





------解决方案--------------------
C# code
 XmlDocument xd;
        XmlNodeList xnl = xd.GetElementsByTagName("UPSInfo");
        for(int i=0;i<xnl.Count;i++)
        {
            if(xnl[i].ChildNodes[0].InnerText == "山特2")
            {
                xnl[i].ParentNode.RemoveChild(xnl[i]);
                break;
            }
        }

------解决方案--------------------
XmlDocument xml=new XmlDocument();
xml.LoadXml(文档载入);
XmlElement root=xml.DocumentElement;
XmlNodeList nodes=root.GetElementsByTagName("UPSInfos");
for(int i=nodes.Count-1;i>=0;i--)
{
if(nodes[i].ChildNodes[0].InnerText=="山特2"&&nodes[i].ChildNodes[1].InnerText=="山特"&&nodes[i].ChildNodes[0].InnerText=="232")
{
root.RemoveChild(nodes[i]);
break;
}

}
------解决方案--------------------
C# code

(1)新建一个网站,其主页默认为Default.aspx。

  (2)在Default.aspx页面中添加一个Xml控件,用来显示XML文件中的内容,然后添加一个TextBox控件和一个Button控件,分别用来输入要删除的XML节点名和执行删除操作。

  (3)程序主要代码如下。

  当单击【删除】按钮时,程序首先判断TextBox1文本框中内容是否为空,如果不为空,则根据TextBox1文本框中内容在XML文件中找到对应节点,并通过XmlElement类的RemoveChild方法将该节点删除,否则,弹出“请输入要删除的节点”信息提示框,并将网页重新定向到该页面。【删除】按钮的Click事件代码如下:

protectedvoidButton1_Click(objectsender,EventArgse)
  {
    if(TextBox1.Text.Trim()!="")
    {
      XmlDocumentdoc=newXmlDocument();
      doc.Load(Server.MapPath("test.xml"));
      XmlNodeListnodes;
      XmlElementroot=doc.DocumentElement;
      nodes=root.SelectNodes("descendant::BOOK[TITLE='"+TextBox1.Text.Trim()+"']");
      foreach(XmlNodenodeinnodes)
      {
        root.RemoveChild(node);
      }
      TextBox1.Text="";
      Response.Write("<script>alert('删除成功')</script>");
      doc.Save(Server.MapPath("test.xml"));
      XslTransformtrans=newXslTransform();
      trans.Load(Server.MapPath("test.xsl"));
      Xml1.Document=doc;
      Xml1.Transform=trans;
    }
    else
      Response.Write("<script>alert('请输入要删除的节点');location='javascript:history.go(-1)';</script>");
  }

百度一下有很多

------解决方案--------------------
[code=C#]
using System.Xml.Linq;
using System.Xml.XPath;

XElement e = XElement.Parse(xmlString);
XElement e1 = e.XPathSelectElement( "Device/UPSInfos/UPSInfo[UpsName= '钢笔2 '] ");
if(e1!=null) e1.Remove();

[/code]
------解决方案--------------------
C# code

        System.Xml.XmlDocument doc;
        public void RemoveUPS(string upsName)
        {
            try
            {
                System.Xml.XmlElement upsInfos = doc["Dev