Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XML
| Search | Today's Posts | Mark Forums Read
XML General XML discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XML section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 13th, 2007, 09:13 AM
Authorized User
 
Join Date: Nov 2007
Location: Allahabad, UP, India.
Posts: 33
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to delete a xml node with all attributes and s

Hello every one! I have a little problem of deleting xml node. I have deleted successfully the

attributes of node <ScreenSupportItem> but opening and closing tag of node was not deleted

<ScreenSupportItem> and </ScreenSupportItem>.
But i have to delete also the opening and clsing tag ofthe node <ScreenSupportItem> means I have to

remove every thing of <ScreenSupportItem>.How can I do this.

following is the structure of my xml file
--------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinkInformation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <LinkToolId>0</LinkToolId>
  <LinkToolVersion>1.0.1</LinkToolVersion>
<ScreensToSupport>
    <ScreenToSupport>
      <ScreenUniqueId>c1f13323-209b-4b0e-ad8d-d34f64d34c28</ScreenUniqueId>
    <SupportItems>
        <ScreenSupportItem>
          <LinkDate>2007-10-25T11:47:10.515625+05:30</LinkDate>
          <LinkAuthor>SUBHASH\Login Subhash</LinkAuthor>
          <LinkURL>C:\Documents and Settings\Login Subhash\Desktop\main.html</LinkURL>
          <LinkText>Adding New Html Help</LinkText>
          <OptionalSupportInstruction />
          <RefId>3</RefId>
        </ScreenSupportItem>
    <ScreenSupportItem>
          ...
        </ScreenSupportItem>
    <ScreenSupportItem>
          ....
        </ScreenSupportItem>
    </SupportItems>
</ScreensToSupport>
</LinkInformation>
--------------------------------------------------------
following is my c# code
--------------------------------------------------------
//int intRefId = Convert.ToInt32(Request.QueryString["RefId"]);
int intRefId = 3;
string strXPath =

"/LinkInformation/ScreensToSupport/ScreenToSupport/SupportItems/ScreenSupportItem[RefId='" +

intRefId + "']";

                XmlNode newParent = doc.SelectSingleNode(strXPath);
                XmlNodeList newChildNodeList = newParent.ParentNode.ChildNodes;
                foreach (XmlNode node1 in newChildNodeList)
                    {
                        foreach (XmlNode node in node1)
                        {
                            if (node.Name == "LinkURL" && node.InnerText == hdntxtUrlLink.Text)
                            {
                                intCount++;
                            }
                            if (node.Name == "LinkText" && node.InnerText == hdntxtUrlTitle.Text)
                            {
                                intCount++;
                            }
                        }
                        if (intCount == 2)
                        {
                            node1.RemoveAll();// This line deleted successfully the node but it left start and end tag of the node <ScreenSupportItem> and </ScreenSupportItem>
                            lblError.Text = "Deleted successfully !";
                            break;
                        }
                    }
                    doc.Save(strXMLFile);
--------------------------------------------------------

 
Old November 13th, 2007, 09:50 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

To delete a node you need to remove it from its parent.

node1.ParentNode.RemoveChild(node1);

I really would recommend getting a book or something as these are fairly elementary questions you are asking.

You could try searching for a Xml .Net tutorial, like this one:
http://www.topxml.com/dotnet/ or http://www.topxml.com/system_xml/default.asp

/- Sam Judson : Wrox Technical Editor -/
 
Old November 14th, 2007, 04:19 AM
Authorized User
 
Join Date: Nov 2007
Location: Allahabad, UP, India.
Posts: 33
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you sir for referring help link for xml, i will go through these links... it will surely increase my knowledge..




Similar Threads
Thread Thread Starter Forum Replies Last Post
delete XML node in Perl crmpicco Perl 4 November 28th, 2006 12:12 PM
delete XML node from JavaScript - not child crmpicco Javascript How-To 1 April 27th, 2006 11:42 AM
finished javascript function to delete XML node crmpicco Javascript 1 April 20th, 2006 11:19 AM
delete XML node with javascript crmpicco Javascript How-To 5 April 6th, 2006 03:35 AM
how to add or delete a node from an xml documnet sai XML 1 March 22nd, 2004 06:54 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.