?
Solved

Delete node from XML file?

Posted on 2006-11-07
7
Medium Priority
?
485 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 200 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 1000 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 800 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 800 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

765 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