Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / XML / October 2003

Tip: Looking for answers? Try searching our database.

Remove Element and Attributes

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Gerry Viator - 07 Oct 2003 20:28 GMT
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
Oleg Tkachenko - 07 Oct 2003 21:06 GMT
> 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?
Signature

Oleg Tkachenko
http://www.tkachenko.com/blog
Multiconn Technologies, Israel

Gerry Viator - 07 Oct 2003 21:11 GMT
DOM solution - load document into XmlDocument, locate the element and
remove it

This would be fine.  Thanks

Gerry

> > The following is part of XML file, how do I search through the file and
> > remove an
[quoted text clipped - 8 lines]
> continue.
> Which one do you prefer?
MSFT - 08 Oct 2003 04:45 GMT
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

Signature

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Gerry Viator - 08 Oct 2003 13:56 GMT
Thanks everyone,

Gerry

> Thank for Oleg's Idea. I attach some sample code as the supply to his post:
>
[quoted text clipped - 22 lines]
> (This posting is provided "AS IS", with no warranties, and confers no
> rights.)
Gerry Viator - 08 Oct 2003 14:09 GMT
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

> Thank for Oleg's Idea. I attach some sample code as the supply to his post:
>
[quoted text clipped - 22 lines]
> (This posting is provided "AS IS", with no warranties, and confers no
> rights.)
Oleg Tkachenko - 08 Oct 2003 14:22 GMT
> What if I wanted to change the value below of TEST2?
>
[quoted text clipped - 3 lines]
>
> 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-us/cpguide/html/cpconmodifyingnodescontentv
aluesinxmldocument.asp

[2]
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconinsertingnodesintoxmld
ocument.asp

[3]
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconselectnodesusingxpathn
avigation.asp

Signature

Oleg Tkachenko
http://www.tkachenko.com/blog
Multiconn Technologies, Israel

Gerry Viator - 08 Oct 2003 14:47 GMT
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

> > What if I wanted to change the value below of TEST2?
> >
[quoted text clipped - 11 lines]
>
> [1]

http://msdn.microsoft.com/library/en-us/cpguide/html/cpconmodifyingnodescontentv
aluesinxmldocument.asp

> [2]

http://msdn.microsoft.com/library/en-us/cpguide/html/cpconinsertingnodesintoxmld
ocument.asp

> [3]

http://msdn.microsoft.com/library/en-us/cpguide/html/cpconselectnodesusingxpathn
avigation.asp

Ice - 09 Oct 2003 17:54 GMT
This stuff should be in the framework sdk.

ice
> Thanks for links,
>
[quoted text clipped - 23 lines]
> >
> > [1]

http://msdn.microsoft.com/library/en-us/cpguide/html/cpconmodifyingnodescontentv
aluesinxmldocument.asp

> > [2]

http://msdn.microsoft.com/library/en-us/cpguide/html/cpconinsertingnodesintoxmld
ocument.asp

> > [3]

http://msdn.microsoft.com/library/en-us/cpguide/html/cpconselectnodesusingxpathn
avigation.asp


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.