469,591 Members | 1,895 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,591 developers. It's quick & easy.

Remove Element and Attributes

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="TEST1">
<Width>600</Width>
<Height>400</Height>
<Tag>2-1</Tag>
</Form>
<Form Name="TEST2">
<Width>200</Width>
<Height>26</Height>
<Tag>2-3</Tag>
</Form>
<Form Name="TEST3">
<Width>300</Width>
<Height>45</Height>
<Tag>2-4</Tag>
</Form>
</Forms>

To look like this?

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

Thanks
Gerry
Nov 11 '05 #1
8 10199
Gerry Viator wrote:
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.


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

Nov 11 '05 #2
DOM solution - load document into XmlDocument, locate the element and
remove it

This would be fine. Thanks

Gerry
"Oleg Tkachenko" <oleg@NO_SPAM_PLEASEtkachenko.com> wrote in message
news:eJ**************@TK2MSFTNGP12.phx.gbl...
Gerry Viator wrote:
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.


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

Nov 11 '05 #3
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.)

Nov 11 '05 #4
Thanks everyone,

Gerry

"MSFT" <lu******@online.microsoft.com> wrote in message
news:H4**************@cpmsftngxa06.phx.gbl...
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.)

Nov 11 '05 #5
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
"MSFT" <lu******@online.microsoft.com> wrote in message
news:H4**************@cpmsftngxa06.phx.gbl...
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.)

Nov 11 '05 #6
Gerry Viator wrote:
What if I wanted to change the value below of TEST2?

<Width>300</Width>

to 350?

and add this to TEST2?


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...mldocument.asp
[2]
http://msdn.microsoft.com/library/en...mldocument.asp
[3]
http://msdn.microsoft.com/library/en...navigation.asp
--
Oleg Tkachenko
http://www.tkachenko.com/blog
Multiconn Technologies, Israel

Nov 11 '05 #7
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

"Oleg Tkachenko" <oleg@NO_SPAM_PLEASEtkachenko.com> wrote in message
news:Od**************@TK2MSFTNGP12.phx.gbl...
Gerry Viator wrote:
What if I wanted to change the value below of TEST2?

<Width>300</Width>

to 350?

and add this to TEST2?
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...mldocument.asp [2]
http://msdn.microsoft.com/library/en...mldocument.asp [3]
http://msdn.microsoft.com/library/en...navigation.asp --
Oleg Tkachenko
http://www.tkachenko.com/blog
Multiconn Technologies, Israel

Nov 11 '05 #8
Ice
This stuff should be in the framework sdk.

ice
"Gerry Viator" <vi*****@musc.edu> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
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

"Oleg Tkachenko" <oleg@NO_SPAM_PLEASEtkachenko.com> wrote in message
news:Od**************@TK2MSFTNGP12.phx.gbl...
Gerry Viator wrote:
What if I wanted to change the value below of TEST2?

<Width>300</Width>

to 350?

and add this to TEST2?


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...mldocument.asp
[2]

http://msdn.microsoft.com/library/en...mldocument.asp
[3]

http://msdn.microsoft.com/library/en...navigation.asp
--
Oleg Tkachenko
http://www.tkachenko.com/blog
Multiconn Technologies, Israel


Nov 11 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by sam.stamport | last post: by
2 posts views Thread by Tom Kerigan | last post: by
1 post views Thread by Tod Johnson | last post: by
14 posts views Thread by kelvin.jones | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.