473,408 Members | 2,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Removing an XML element from an XML document. How can I do that?

This is my XML file and below you will find my code to remove it... I was
thinking that it's a simple task but this code doesn't work.

Anybody have a better solution?

<Advertisements>

<Ad>

<ImageUrl>http://localhost/UQCN/Images/Ecoroute.gif</ImageUrl>

<NavigateUrl>/UQCN/WebModules/AdsManager/PassThrough.aspx?AD=4</NavigateUrl>

<AlternateText>Test!</AlternateText>

<Keyword>Wrox</Keyword>

<Impressions>1</Impressions>

</Ad>

----------Here is the element I want to remove from my XML doc

<Ad>

<ImageUrl>http://localhost/UQCN/Images/foretboreale.jpg</ImageUrl>

<NavigateUrl>/UQCN/WebModules/AdsManager/PassThrough.aspx?AD=5</NavigateUrl>

<AlternateText>Test 2</AlternateText>

<Keyword>Wrox2</Keyword>

<Impressions>5</Impressions>

</Ad>

---------------------------------------End of removed
element--------------------------------

<Ad>

<ImageUrl>http://localhost/UQCN/Images/Aptitudes.jpg</ImageUrl>

<NavigateUrl>/UQCN/WebModules/AdsManager/PassThrough.aspx?AD=6</NavigateUrl>

<AlternateText>tytvytv</AlternateText>

<Keyword>1</Keyword>

<Impressions>1</Impressions>

</Ad>

</Advertisements>

Here is my Code

Dim rotatorDom As New XmlDocument()

rotatorDom.Load(rotatorFile)

Dim xPathFind As String = "//Ad[NavigateUrl=""" & _

settings.PassThroughPage & "?AD=" & _

advertisementId.ToString() & """]"

rotatorDom.Load(rotatorFile)

Dim currentAd As XmlNode = _

CType(rotatorDom.DocumentElement.SelectSingleNode( xPathFind), XmlNode)

rotatorDom.DocumentElement.RemoveChild(currentAd)

My file still jam with this element!!!! Nothing changes


Nov 18 '05 #1
3 1211
I would think what you have would work. Have you tried debugging it to see
what is happening? Also, when you say "My file still jam with this
element", by file are you refering to a text file that contains your XML or
the XMLDocument object in your code? If you debug, have you tried
outputting the rotatorDom.DocumentElement.OuterXML in the command window to
see what it has after trying the remove? One other way you could try the
remove would be to get the parent of the node you want to remove instead of
going back to the XmlDoc object, so:

replace: rotatorDom.DocumentElement.RemoveChild(currentAd)
with: currentAd.ParentNode.RemoveChild(currentAd)
One other thought, are you certain that the Xpath string you generate is
actually finding a node? if currentAd = nothing I'm not sure if the
RemoveChild would raise an exception or just not do anything. That would be
one other thing to check.
==============
Joe Reazor
Gorbel Inc.
email: joerea=AT=gorbel=DOT=com
"GhislainTanguay" <fr***************@hotmail.com> wrote in message
news:Oc**************@TK2MSFTNGP11.phx.gbl...
This is my XML file and below you will find my code to remove it... I was
thinking that it's a simple task but this code doesn't work.

Anybody have a better solution?

<Advertisements>

<Ad>

<ImageUrl>http://localhost/UQCN/Images/Ecoroute.gif</ImageUrl>

<NavigateUrl>/UQCN/WebModules/AdsManager/PassThrough.aspx?AD=4</NavigateUrl>
<AlternateText>Test!</AlternateText>

<Keyword>Wrox</Keyword>

<Impressions>1</Impressions>

</Ad>

----------Here is the element I want to remove from my XML doc

<Ad>

<ImageUrl>http://localhost/UQCN/Images/foretboreale.jpg</ImageUrl>

<NavigateUrl>/UQCN/WebModules/AdsManager/PassThrough.aspx?AD=5</NavigateUrl>
<AlternateText>Test 2</AlternateText>

<Keyword>Wrox2</Keyword>

<Impressions>5</Impressions>

</Ad>

---------------------------------------End of removed
element--------------------------------

<Ad>

<ImageUrl>http://localhost/UQCN/Images/Aptitudes.jpg</ImageUrl>

<NavigateUrl>/UQCN/WebModules/AdsManager/PassThrough.aspx?AD=6</NavigateUrl>
<AlternateText>tytvytv</AlternateText>

<Keyword>1</Keyword>

<Impressions>1</Impressions>

</Ad>

</Advertisements>

Here is my Code

Dim rotatorDom As New XmlDocument()

rotatorDom.Load(rotatorFile)

Dim xPathFind As String = "//Ad[NavigateUrl=""" & _

settings.PassThroughPage & "?AD=" & _

advertisementId.ToString() & """]"

rotatorDom.Load(rotatorFile)

Dim currentAd As XmlNode = _

CType(rotatorDom.DocumentElement.SelectSingleNode( xPathFind), XmlNode)

rotatorDom.DocumentElement.RemoveChild(currentAd)

My file still jam with this element!!!! Nothing changes

Nov 18 '05 #2
Tks, I will try it.

For sure when I check my watch for my currentAd node, i saw the node I want
to remove and when I check for my rotatorDom, i saw all my XML file int it
so... I think i'm on the good track but my AdRotator.xml file still with
this $?%?% node!!!! I will try your
currentAd.ParentNode.RemoveChild(currentAd).

Tks for your quick answer buddy

"Joe Reazor" <jo*******@belgor.com> a écrit dans le message de
news:O8**************@TK2MSFTNGP12.phx.gbl...
I would think what you have would work. Have you tried debugging it to see what is happening? Also, when you say "My file still jam with this
element", by file are you refering to a text file that contains your XML or the XMLDocument object in your code? If you debug, have you tried
outputting the rotatorDom.DocumentElement.OuterXML in the command window to see what it has after trying the remove? One other way you could try the
remove would be to get the parent of the node you want to remove instead of going back to the XmlDoc object, so:

replace: rotatorDom.DocumentElement.RemoveChild(currentAd)
with: currentAd.ParentNode.RemoveChild(currentAd)
One other thought, are you certain that the Xpath string you generate is
actually finding a node? if currentAd = nothing I'm not sure if the
RemoveChild would raise an exception or just not do anything. That would be one other thing to check.
==============
Joe Reazor
Gorbel Inc.
email: joerea=AT=gorbel=DOT=com
"GhislainTanguay" <fr***************@hotmail.com> wrote in message
news:Oc**************@TK2MSFTNGP11.phx.gbl...
This is my XML file and below you will find my code to remove it... I was thinking that it's a simple task but this code doesn't work.

Anybody have a better solution?

<Advertisements>

<Ad>

<ImageUrl>http://localhost/UQCN/Images/Ecoroute.gif</ImageUrl>

<NavigateUrl>/UQCN/WebModules/AdsManager/PassThrough.aspx?AD=4</NavigateUrl>

<AlternateText>Test!</AlternateText>

<Keyword>Wrox</Keyword>

<Impressions>1</Impressions>

</Ad>

----------Here is the element I want to remove from my XML doc

<Ad>

<ImageUrl>http://localhost/UQCN/Images/foretboreale.jpg</ImageUrl>

<NavigateUrl>/UQCN/WebModules/AdsManager/PassThrough.aspx?AD=5</NavigateUrl>

<AlternateText>Test 2</AlternateText>

<Keyword>Wrox2</Keyword>

<Impressions>5</Impressions>

</Ad>

---------------------------------------End of removed
element--------------------------------

<Ad>

<ImageUrl>http://localhost/UQCN/Images/Aptitudes.jpg</ImageUrl>

<NavigateUrl>/UQCN/WebModules/AdsManager/PassThrough.aspx?AD=6</NavigateUrl>

<AlternateText>tytvytv</AlternateText>

<Keyword>1</Keyword>

<Impressions>1</Impressions>

</Ad>

</Advertisements>

Here is my Code

Dim rotatorDom As New XmlDocument()

rotatorDom.Load(rotatorFile)

Dim xPathFind As String = "//Ad[NavigateUrl=""" & _

settings.PassThroughPage & "?AD=" & _

advertisementId.ToString() & """]"

rotatorDom.Load(rotatorFile)

Dim currentAd As XmlNode = _

CType(rotatorDom.DocumentElement.SelectSingleNode( xPathFind), XmlNode)

rotatorDom.DocumentElement.RemoveChild(currentAd)

My file still jam with this element!!!! Nothing changes


Nov 18 '05 #3
Ok, based on what I think you just said, you might be missing a step, as I
thought. If you update your XMLDocument object, rotatorDom, then the
underlying AdRotator.xml file is not automatically updated. You have to do
that yourself once you are finished updating the XMLDoc object. There's no
link between the two. There's a few ways to do it, I usually do something
like this:

Private OutputFile As FileStream
Private OutputWriter As StreamWriter

'create the output file however you like, there's several ways
OutputFile = File.Create(path)
OutputWriter = New StreamWriter(OutputFile)
OutputWriter.WriteLine(Results.OuterXml) 'Results is my XmlDocument object
OutputWriter.Flush()
==============
Joe Reazor
Gorbel Inc.
email: joerea=AT=gorbel=DOT=com
"GhislainTanguay" <fr***************@hotmail.com> wrote in message
news:ei*************@tk2msftngp13.phx.gbl...
Tks, I will try it.

For sure when I check my watch for my currentAd node, i saw the node I want to remove and when I check for my rotatorDom, i saw all my XML file int it
so... I think i'm on the good track but my AdRotator.xml file still with
this $?%?% node!!!! I will try your
currentAd.ParentNode.RemoveChild(currentAd).

Tks for your quick answer buddy

"Joe Reazor" <jo*******@belgor.com> a écrit dans le message de
news:O8**************@TK2MSFTNGP12.phx.gbl...
I would think what you have would work. Have you tried debugging it to see
what is happening? Also, when you say "My file still jam with this
element", by file are you refering to a text file that contains your XML

or
the XMLDocument object in your code? If you debug, have you tried
outputting the rotatorDom.DocumentElement.OuterXML in the command window

to
see what it has after trying the remove? One other way you could try the
remove would be to get the parent of the node you want to remove instead

of
going back to the XmlDoc object, so:

replace: rotatorDom.DocumentElement.RemoveChild(currentAd)
with: currentAd.ParentNode.RemoveChild(currentAd)
One other thought, are you certain that the Xpath string you generate is
actually finding a node? if currentAd = nothing I'm not sure if the
RemoveChild would raise an exception or just not do anything. That

would be
one other thing to check.
==============
Joe Reazor
Gorbel Inc.
email: joerea=AT=gorbel=DOT=com
"GhislainTanguay" <fr***************@hotmail.com> wrote in message
news:Oc**************@TK2MSFTNGP11.phx.gbl...
This is my XML file and below you will find my code to remove it... I

was thinking that it's a simple task but this code doesn't work.

Anybody have a better solution?

<Advertisements>

<Ad>

<ImageUrl>http://localhost/UQCN/Images/Ecoroute.gif</ImageUrl>

<NavigateUrl>/UQCN/WebModules/AdsManager/PassThrough.aspx?AD=4</NavigateUrl>

<AlternateText>Test!</AlternateText>

<Keyword>Wrox</Keyword>

<Impressions>1</Impressions>

</Ad>

----------Here is the element I want to remove from my XML doc

<Ad>

<ImageUrl>http://localhost/UQCN/Images/foretboreale.jpg</ImageUrl>

<NavigateUrl>/UQCN/WebModules/AdsManager/PassThrough.aspx?AD=5</NavigateUrl>

<AlternateText>Test 2</AlternateText>

<Keyword>Wrox2</Keyword>

<Impressions>5</Impressions>

</Ad>

---------------------------------------End of removed
element--------------------------------

<Ad>

<ImageUrl>http://localhost/UQCN/Images/Aptitudes.jpg</ImageUrl>

<NavigateUrl>/UQCN/WebModules/AdsManager/PassThrough.aspx?AD=6</NavigateUrl>

<AlternateText>tytvytv</AlternateText>

<Keyword>1</Keyword>

<Impressions>1</Impressions>

</Ad>

</Advertisements>

Here is my Code

Dim rotatorDom As New XmlDocument()

rotatorDom.Load(rotatorFile)

Dim xPathFind As String = "//Ad[NavigateUrl=""" & _

settings.PassThroughPage & "?AD=" & _

advertisementId.ToString() & """]"

rotatorDom.Load(rotatorFile)

Dim currentAd As XmlNode = _

CType(rotatorDom.DocumentElement.SelectSingleNode( xPathFind), XmlNode)

rotatorDom.DocumentElement.RemoveChild(currentAd)

My file still jam with this element!!!! Nothing changes



Nov 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jim Ley | last post by:
Hi, IE has the ability to setExpressions on stylesheets so you can calculate the value of the css property through script. For various reasons I'm wanting to use a side-effect of this to...
2
by: Asad Khan | last post by:
I have an HTML file that has a call to a Javascript function in it as follows: <!-- bunch of stuff --> <script type="text/javascript">doXMLFromString()</script> <!-- bunch of stuff --> Now I...
2
by: Raja Kannan | last post by:
Is there a way to remove text portion from the HTML keeping the HTML Tags using the browser, say javascript RegEx or something ? I have seen lot of examples removing HTML tags to get the text...
2
by: bmgz | last post by:
I have written a simple function that validates a form based on the form objects' className attribute. The form basically write a "field required" message next to the form element that is blank(and...
2
by: Greg | last post by:
Hi. I have a rather large xml document (object) that can have one or more nodes with a certain attribute throughout (at ANY depth, not at the same level necessarily). I need to find this...
24
by: RyanTaylor | last post by:
I have a final coming up later this week in my beginning Java class and my prof has decided to give us possible Javascript code we may have to write. Problem is, we didn't really cover JS and what...
3
by: willwade | last post by:
Hi there, Sorry for the perhaps undirected post - Ive struggled for the past few hours and cant see the wood for the trees.. I have a page (infact a site but I will keep it simple) that is a...
6
by: Peter Michaux | last post by:
Hi, Douglas Crockford mentioned in a video on Yahoo! that before removing an element it is a good idea to purge it's event handlers. I think he was only refering to the event handlers defined...
6
by: bgold12 | last post by:
Hey, I just want to make sure that when I remove an element I don't have to worry about the events listeners I added previously to the element. For example: // get the element by its id elem =...
0
by: Alien | last post by:
I came across this problem where I cant remove element from the document. Here is my code. DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder builder =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.