Groups | Blog | Home
all groups > dotnet xml > january 2006 >

dotnet xml : Need a few examples


Billg_sd
1/10/2006 4:08:02 PM
It's been awhile since I've had to work with XML in VB.Net. Look at the
following snippet:

<Parameters>
<Parameter>123</Parameter>
<Parameter>456</Parameter>
</Parameters>

How do I find Parameter=123 and delete it?
How do I delete all children of <Parameters>?

Assume I read this XML in from a file and wish to write the results of my
changes out to a different file. Is this simpler to do in .Net 2.0 than in
..Net 1.5?

Even if specific examples aren't available, .net's handling of XML is quite
overwhelming: DOM, XPATH, ADO.NET-->relational???

Thanks,

Martin Honnen
1/11/2006 3:39:02 PM


[quoted text, click to view]


[quoted text, click to view]

Dim Xml_Document As XmlDocument = New XmlDocument
Xml_Document.PreserveWhitespace = True
Xml_Document.Load(Args(0))

Dim Parameter As XmlNode =
Xml_Document.SelectSingleNode("Parameters/Parameter[. = 123]")
If Not Parameter Is Nothing Then
Parameter.ParentNode.RemoveChild(Parameter)
End If

Xml_Document.Save(Console.Out)

Of course instead of doing Save(Console.Out) you could also save to some
file.

[quoted text, click to view]


Dim Xml_Document As XmlDocument = New XmlDocument
Xml_Document.PreserveWhitespace = True
Xml_Document.Load(Args(0))

Dim Parameters As XmlNodeList =
Xml_Document.SelectNodes("Parameters/node()")

For Each Child As XmlNode In Parameters
Child.ParentNode.RemoveChild(Child)
Next

Xml_Document.Save(Console.Out)

[quoted text, click to view]

There is no .NET 1.5, but saving to a file is as simple as
Xml_Document.Save("file.xml")
whether you use .NET 1.x or .NET 2.0.
There haven't been any major changes in System.Xml or
System.Xml.XmlDocument when it comes to loading and saving.



--

Martin Honnen --- MVP XML
AddThis Social Bookmark Button