[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Delete node from XML file?

Posted on 2006-11-07
7
Medium Priority
?
492 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

656 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