Groups | Blog | Home
all groups > dotnet xml > october 2003 >

dotnet xml : Remove Element and Attributes



Gerry Viator
10/7/2003 3:28:44 PM
Hi all,

The following is part of XML file, how do I search through the file and =
remove an
Element and all it's attributes. Example: TEST2? Of course I will pass =
the Element Name to remove
from a control.



- <Forms>
- <Form Name=3D"TEST1">
<Width>600</Width>=20
<Height>400</Height>=20
<Tag>2-1</Tag>=20
</Form>
<Form Name=3D"TEST2">
<Width>200</Width>=20
<Height>26</Height>=20
<Tag>2-3</Tag>=20
</Form>
<Form Name=3D"TEST3">
<Width>300</Width>=20
<Height>45</Height>=20
<Tag>2-4</Tag>=20
</Form>
</Forms>

To look like this?

<Forms>
- <Form Name=3D"TEST1">
<Width>600</Width>=20
<Height>400</Height>=20
<Tag>2-1</Tag>=20
</Form>
<Form Name=3D"TEST3">
<Width>300</Width>=20
<Height>45</Height>=20
<Tag>2-4</Tag>=20
</Form>
</Forms>

Thanks
Gerry Viator
10/7/2003 4:11:54 PM
DOM solution - load document into XmlDocument, locate the element and
remove it

This would be fine. Thanks

Gerry


[quoted text, click to view]

Oleg Tkachenko
10/7/2003 10:06:13 PM
[quoted text, click to view]

DOM solution - load document into XmlDocument, locate the element and
remove it.
XSLT solution - identity transfomation + custom template for Form element.
XmlReader solution - read till the element, skip till EndElement and
continue.
Which one do you prefer?
--
Oleg Tkachenko
http://www.tkachenko.com/blog
Multiconn Technologies, Israel
lukezhan NO[at]SPAM online.microsoft.com
10/8/2003 3:45:31 AM
Thank for Oleg's Idea. I attach some sample code as the supply to his post:

Dim xmldoc As New XmlDocument

xmldoc.Load("c:\edit1.xml")

Dim xmlnode As XmlNode

For Each xmlnode In xmldoc.DocumentElement.ChildNodes

If xmlnode.Attributes("Name").Value = "TEST2" Then
xmldoc.DocumentElement.RemoveChild(xmlnode)

End If
Next

xmldoc.Save("c:\edit2.xml")

Gerry, Hope this answer your question.

Luke
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
Gerry Viator
10/8/2003 8:56:56 AM
Thanks everyone,

Gerry



[quoted text, click to view]

Gerry Viator
10/8/2003 9:09:16 AM
Thanks again,

What if I wanted to change the value below of TEST2?

<Width>300</Width>

to 350?

and add this to TEST2?

<Location>True</Location>

thanks for your help
Gerry


[quoted text, click to view]

Gerry Viator
10/8/2003 9:47:33 AM
Thanks for links,

I also wish Microsoft would make there HELP much better then it is.
On line and within VS.NET. I always search first, but get
frustrated with not findings what I'm looking for.

Microsofts Help can be made alot better. I do beleive they are going to make
major changes to it from what I hear.

Thanks
Gerry

[quoted text, click to view]

Oleg Tkachenko
10/8/2003 3:22:06 PM
[quoted text, click to view]

Gerry, to avoid asking such simple questions learn about using XmlDocument
class, e.g.
"Modifying Nodes, Content, and Values in an XML Document" [1]
"Inserting Nodes into an XML Document" [2]
"Select Nodes Using XPath Navigation" [3]

[1]
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconmodifyingnodescontentvaluesinxmldocument.asp
[2]
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconinsertingnodesintoxmldocument.asp
[3]
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconselectnodesusingxpathnavigation.asp
--
Oleg Tkachenko
http://www.tkachenko.com/blog
Multiconn Technologies, Israel
Ice
10/9/2003 11:54:42 AM
This stuff should be in the framework sdk.

ice
[quoted text, click to view]

AddThis Social Bookmark Button