Solved

Delete node from XML file?

Posted on 2006-11-07
7
479 Views
Last Modified: 2008-01-09
Hi,
How can I delete "user" node and all its children nodes from the following XML file depending on the id that comes as input parameter:

<Marks>
     <user>
         <name> yyy</name>
          <id>1</id>
          <mark A="11" B="12" />
     </user>
     <user>
           <name> zzz</name>
           <id>2</id>
           <mark A="34" B="50" />
     </user>
</Marks>


For example if lngID variable is equal 2 I want to delete the following noed:
  <user>
           <name> zzz</name>
           <id>2</id>
           <mark A="34" B="50" />
  </user>


Please help.
0
Comment
Question by:Abdu_Allah
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 4

Assisted Solution

by:satish_nagdev
satish_nagdev earned 50 total points
ID: 17888274
hi,
see this link ==> http://p2p.wrox.com/topic.asp?TOPIC_ID=44436 should be helpful.

regards,
satish.
0
 
LVL 11

Expert Comment

by:Vivek Thangaswamy
ID: 17888530
0
 
LVL 6

Accepted Solution

by:
Niraj_Singh earned 250 total points
ID: 17888593
Dim doc as XmlDocument = new XmlDocument()
doc.Load("doc.xml");

doc.DocumentElement.RemoveChild(root.DocumentElement.SelectSingleNode("descendant::id[text()'2']").ParentNode);

0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 3

Author Comment

by:Abdu_Allah
ID: 17890002
Guys please do not point me to another resource I just need source code solve my problem.
0
 
LVL 11

Assisted Solution

by:Vivek Thangaswamy
Vivek Thangaswamy earned 200 total points
ID: 17890247
Then here is the code to remove the node

public int Delete(string path)
{
  if (path==null)
  {
    throw(new ArgumentNullException("path cannot be null."));
  }

  XmlNodeList nodeList=xdoc.SelectNodes(rootName+"/"+path);
  foreach(XmlNode node in nodeList)
  {
    node.ParentNode.RemoveChild(node);
  }
  return nodeList.Count;
}
0
 
LVL 3

Author Comment

by:Abdu_Allah
ID: 17890289
I do not want to loop through entire xml file, I want to use SelectSingleNode function, I do it like this but it does not delete that node:
        doc.Load(Server.MapPath("Data.xml"))
        doc.DocumentElement.RemoveChild(doc.DocumentElement.SelectSingleNode("descendant::id[text()='7']").ParentNode)
0
 
LVL 11

Assisted Solution

by:Vivek Thangaswamy
Vivek Thangaswamy earned 200 total points
ID: 17890679
XmlNode n1 = doc.SelectSingleNode("//Price");
XmlNode n2 = doc.SelectNodes("//Price")[0];

You'd think that it would be more efficient to use XmlNode's SelectSingleNode than SelectNodes since the latter returns an XmlNodeList while the former returns only the first XmlNode that matches. Due to constraints in the DOM specification, the XmlNodeList implementation should come with more overhead than just accessing the first matching node. However, thanks to the Microsoft implementation, there isn't a significant difference between the two techniques.

For instance, consider the internal code for the call to SelectSingleNode, which (using ILDasm or anakrino) looks something like this:

public XmlNode SelectSingleNode(string xpath) {
    XmlNodeList local0;
    XmlNode local1;

    try {
        local0 = this.SelectNodes(xpath);
        local1 = local0.get_ItemOf(0);
    }
    catch (ArgumentOutOfRangeException) {
        local1 = null;
    }
    return local1;
}

Notice that the code simply delegates to SelectNodes and returns the first item in the local XmlNodeList object. You may be wondering why they did something so seemingly foolish, but before judging too quickly let's take a close look at the actual implementation of SelectNodes:
public XmlNodeList SelectNodes(string xpath) {
    XPathNavigator local0;

    local0 = this.CreateNavigator();
    return new XPathNodeList(local0.Select(xpath));
}

Note that the code creates an XPathNavigator for the current DOM tree, calls Select, and returns an XPathNodeList object (which happens to derive from XmlNodeList). The implementation of XPathNodeList uses XPathNavigator's traversal methods to move through the selection as they're requested, and along the way it caches the encountered XmlNode references just in case they're asked for again.


0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question