Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Delete node from XML file?

Posted on 2006-11-07
7
Medium Priority
?
494 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 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

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.

Question has a verified solution.

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

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…
Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

810 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