2010-09-28 47 views
0


我想将所有已删除的元素属性写入xml文档以保存在数据库中。当用户删除已删除的元素时,我需要从Xml文档中删除该元素。根据用户输入删除XML节点

XmlDocument oXmlDocument = new XmlDocument(); 
    oXmlDocument.Load(@"D:\VanithaApps\SenMail\DiagramData.xml"); 
    Boolean nodeExits = false; 
    XmlNode oXmlRootNode = oXmlDocument.SelectSingleNode("records");  
    XmlNodeList xmlnode = oXmlDocument.GetElementsByTagName("record"); 

    if (delete=="1") 
    { 
     if (xmlnode.Count > 0) 
     { 
      for (int i = 0; i < xmlnode.Count; i++) 
      { 
       string tempVar = element.Substring(0, element.Length - 1); 
       if (xmlnode[i].ChildNodes[2].InnerText == tempVar) 
       { 
        try 
        { 
         oXmlRootNode.RemoveChild(xmlnode[i]); 
         goto Found; 
        } 
        catch(Exception ex) 
        { 
         ex.ToString(); 
        } 

       } 
      } 
     } 
    } 

    if (xmlnode.Count > 0) 
    { 
     for (int i = 0; i < xmlnode.Count; i++) 
     { 
      string tempVar = element.Substring(0, element.Length-1); 
      if (xmlnode[i].ChildNodes[2].InnerText == tempVar) 
      { 
       nodeExits = true; 
       XmlNode XAxis = xmlnode[i].ChildNodes[0]; 
       XAxis.InnerText = Convert.ToString(x); 

       XmlNode YAxis = xmlnode[i].ChildNodes[1]; 
       YAxis.InnerText = Convert.ToString(y); 
      } 

     } 
     if (nodeExits == false) 
      { 
       CreateNewNode(x, y, element, userid, oXmlDocument, oXmlRootNode); 

      } 
    } 

    else 
    { 
     CreateNewNode(x, y, element, userid, oXmlDocument, oXmlRootNode); 
    } 

    Found: 

    int result = 0; 
    return result; 

我之前广泛
<?xml version="1.0" encoding="utf-8"?> <records> <record> <X-Cordinate>774</X-Cordinate> <Y-Cordinate>173</Y-Cordinate> <Element>drag595</Element> <UserID>1</UserID> </record> </records>
我想删除其孩子没有使用XML“元素”值等于tempVar.Here如果元素值等于drag595,我想删除从我的XML输入。

+0

我不确定那里有'goto Found;'在那里做什么。请告诉我这是一个错字,否则[猛禽会吃掉你](http://xkcd.com/292/)。 – Tomalak 2010-09-28 17:53:32

+0

转到找到就像是在打破案件stmt – Vani 2010-09-29 14:23:22

+0

号'goto'完全没有任何理由使用'goto',你绝对不应该这样做。 – Tomalak 2010-09-29 14:36:10

回答

0

变化

oXmlRootNode.RemoveChild(xmlnode[i]);//not working// 

xmlnode[i].ParentNode.RemoveChild(xmlnode[i]); 

儿童只能从他们各自的父母被删除。

+0

我试过xmlnode [i] .ParentNode.RemoveChild(xmlnode [i]); 它什么都不做,那是我搬到.Removechild.PLease看到我上面完整编辑的代码块。 – Vani 2010-09-29 14:24:44