Login    
 
 
 
 
Text/HTML
  
You are here :: Blogs Saturday, May 19, 2012

Search
Note: This uses the internal blog search engine. The Google search engine is also available at the top of the page.
  
Disclaimer

Please review the site disclaimer before downloading or using content found on this site

  
Categories
  
DEVSHED Blog
As always, I welcome your comments!
Sep 24

Written by: Steve Gray
9/24/2009 6:00 PM  RssIcon

The task is to delete nodes from an XML document – selectively. In other words, we want to look at the document and delete nodes using some sort of criteria.

I had to make two passes through the data, because in the first pass, xElement.Remove() remove the target node, but it exited the loop immediately so it only removed one node. I switched to using xElement.RemoveAll() which has the effect of emptying the node.

In the second pass I use LinqToXML to query empty nodes and delete them.

Public Class Form1
 
    Private Sub Form1_Load(ByVal sender As System.Object, _
         ByVal e As System.EventArgs) Handles MyBase.Load
        Dim strItemNumber As String
 
        'set up the xml doc
        Dim eConnect As XElement = _
            <eConnect>
                <SOPTransactionType>
                    <taSopLineIvcInsert_Items>
                        <taSopLineIvcInsert>
                            <ITEMNMBR>100</ITEMNMBR>
                        </taSopLineIvcInsert>
                        <taSopLineIvcInsert>
                            <ITEMNMBR>101</ITEMNMBR>
                        </taSopLineIvcInsert>
                        <taSopLineIvcInsert>
                            <ITEMNMBR>102</ITEMNMBR>
                        </taSopLineIvcInsert>
                    </taSopLineIvcInsert_Items>
                </SOPTransactionType>
            </eConnect>
 
        'loop through the doc and empty target elements
        For Each SOPTransactionType In eConnect.Elements("SOPTransactionType")
            Dim taSopLineIvcInsert_Items As XElement = _
                SOPTransactionType.Element("taSopLineIvcInsert_Items")
            For Each taSopLineIvcInsert In _
                taSopLineIvcInsert_Items.Elements("taSopLineIvcInsert")
                strItemNumber = taSopLineIvcInsert.Element("ITEMNMBR")
 
                'need to be able to evaluate the contents of the node 
                'and possibly delete it.
                If strItemNumber = "100" Or strItemNumber = "101" Then
                    'Remove won't work, so we use RemoveAll to EMPTY the node
                    taSopLineIvcInsert.RemoveAll()
                End If
            Next
        Next
 
        'now we make a second pass, and query all the empty nodes, 
        Dim x As New Xml.XmlDocument
 
        Dim query = _
            From c In _
eConnect.<SOPTransactionType>.<taSopLineIvcInsert_Items>.<taSopLineIvcInsert> _
            Where c.HasElements = False _
            Select c
 
        'and delete them
        query.Remove()
 
        x.LoadXml(eConnect.ToString)
 
        Me.RichTextBox1.Text = eConnect.ToString
    End Sub
End Class

Tags:
Categories:
As always, I welcome your comments!
  
 
 
Home | Products | Blogs | Contact Us | Links | God's Plan
Privacy Statement | Terms Of Use
 
Copyright 2011 by Devshed.us