Solved

Delete node from XML file?

Posted on 2006-11-07
7
470 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
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
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…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now