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

G

GhislainTanguay

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
 
J

Joe Reazor

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 said:
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>
<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>
<AlternateText>Test 2</AlternateText>

<Keyword>Wrox2</Keyword>

<Impressions>5</Impressions>

</Ad>

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

<Ad>

<ImageUrl>http://localhost/UQCN/Images/Aptitudes.jpg</ImageUrl>
<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
 
G

GhislainTanguay

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 said:
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 said:
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>
<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>
<AlternateText>Test 2</AlternateText>

<Keyword>Wrox2</Keyword>

<Impressions>5</Impressions>

</Ad>

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

<Ad>

<ImageUrl>http://localhost/UQCN/Images/Aptitudes.jpg</ImageUrl>
<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
 
J

Joe Reazor

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 said:
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 said:
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 said:
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>
<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>
<AlternateText>Test 2</AlternateText>

<Keyword>Wrox2</Keyword>

<Impressions>5</Impressions>

</Ad>

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

<Ad>

<ImageUrl>http://localhost/UQCN/Images/Aptitudes.jpg</ImageUrl>
<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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top