Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / June 2007

Tip: Looking for answers? Try searching our database.

How to remove an XML Node

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
mark - 14 Jun 2007 14:18 GMT
Hi, I am having trouble with deleting nodes from an XML file. I use the
following code to remove an XML node.

xNode = xDoc.SelectSingleNode("//country/city/street[@name='" &
ListBox1.SelectedItem.ToString & "']")
xNode.RemoveAll()

<country name="France">
    <city name="Paris">
      <street name="1 France Lane">
        <details>
          <postcode>9988jj</postcode>
          <province></province>
          <c>France</c>
        </details>
      </street>
      <street name="2 France Lane">
        <details>
          <postcode>9988jj</postcode>
          <province></province>
          <c>France</c>
        </details>
      </street>
</country>

The code seems to work except it is not removing the full node. I.E it
leaves behind and empty tag.

For example, if I needed to delete the second street address in the XML
above (2 France Lane) it would leave an empty tag of <street></street>
behind as illustrated below.

<country name="France">
    <city name="Paris">
      <street name="1 France Lane">
        <details>
          <postcode>9988jj</postcode>
          <province></province>
          <c>France</c>
        </details>
      </street>
      <street>
      </street>
</country>

Can anyone advise how I can clean this up and remove the full Street
element?

Many thanks.

Mark.
Martin Honnen - 14 Jun 2007 14:28 GMT
> Hi, I am having trouble with deleting nodes from an XML file. I use the
> following code to remove an XML node.
>
> xNode = xDoc.SelectSingleNode("//country/city/street[@name='" &
> ListBox1.SelectedItem.ToString & "']")
> xNode.RemoveAll()

RemoveAll remove attributes and children but not the node itself. To do
that use e.g.
  xNode.ParentNode.RemoveChild(xNode)

Signature

    Martin Honnen --- MVP XML
    http://JavaScript.FAQTs.com/

mark - 14 Jun 2007 14:40 GMT
Excellent, thanks Martin that worked.

>> Hi, I am having trouble with deleting nodes from an XML file. I use
>> the following code to remove an XML node.
[quoted text clipped - 6 lines]
> that use e.g.
>   xNode.ParentNode.RemoveChild(xNode)

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.