Groups | Blog | Home
all groups > dotnet xml > may 2004 >

dotnet xml : Removing XML Element. Simple??? Give me your trick please


GhislainTanguay
5/21/2004 11:10:21 AM
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



Sonu Kapoor
5/21/2004 2:05:06 PM
I might be wrong but the caller of RemoveChild must be his
parent. I created a small example to demonstrate this:

XML:

<?xml version="1.0" encoding="utf-8"?>
<CategoryList>
<Category ID="01">
<MainCategory>XML</MainCategory>
<Description>This is a list my XML
articles.</Description>
<Active>true</Active>
</Category>
</CategoryList>

Code:

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("categories.xml"));

XmlNode node = xmlDoc.SelectSingleNode
("/CategoryList/Category[@ID='01']");
XmlNode commonParent = node.ParentNode;

if(node != null)
{
commonParent.RemoveChild(node);
}

xmlDoc.Save(Server.MapPath("categories.xml"));


hth

Sonu Kapoor
http://weblogs.asp.net/sonukapoor/

[quoted text, click to view]
GhislainTanguay
5/25/2004 10:18:19 AM
Well tks buddy,
My code is good except that didn't save my change after updating my file
All I needed is
xmlDoc.Save(Server.MapPath("categories.xml"));

Tks again

"Sonu Kapoor" <vikasde@hotmail.com> a écrit dans le message de
news:1076901c43f77$4ae3d5c0$a501280a@phx.gbl...
[quoted text, click to view]

AddThis Social Bookmark Button