Replace All XML Data

  • Thread starter Thread starter Derek Hart
  • Start date Start date
D

Derek Hart

For testing purposes, I need some code that will loop through all attributes
and elements in an XML file, and replace the data with the element or
attribute name? Does anybody have code that will do this, looping
generically through every node? VB.Net code would be appreciated so I can
see how it is done.

Derek
 
Hello Derek,

You could use a recursive function that operated on XmlNode objects (see
XmlNode in MSDN docs or Object Browser). Or you could use an XmlReader and
process each node as it was streamed in. I really have no experience using
XmlNavigator or XmlReader.. so I can only speak about the XmlNode method.

I'll outline the process in pseudo code.. you can give it a shot from there.

Load the XML into an XmlDocument
Pass the root node to the recursive function

The recursive function will take an XmlNode as input.
Function guts:
Process node (read all attributes; read node text)
For each childNode in inputNode.ChildNodes
pass childnode to function
End For
End Function

Enjoy,
-Boo
 
Does anybody have code for this?


GhostInAK said:
Hello Derek,

You could use a recursive function that operated on XmlNode objects (see
XmlNode in MSDN docs or Object Browser). Or you could use an XmlReader
and process each node as it was streamed in. I really have no experience
using XmlNavigator or XmlReader.. so I can only speak about the XmlNode
method.

I'll outline the process in pseudo code.. you can give it a shot from
there.

Load the XML into an XmlDocument
Pass the root node to the recursive function

The recursive function will take an XmlNode as input.
Function guts:
Process node (read all attributes; read node text)
For each childNode in inputNode.ChildNodes
pass childnode to function
End For
End Function

Enjoy,
-Boo
 
Derek,

I doubt if anybody will have code for this specific question of you.

However this is code that shows easily how to use the node reader maybe you
can expiriment with the most inner part in the loop.

\\\\
Dim xmlString As String = "<department>" & _
"<employee name=""ABC"" age=""31"" sex=""male""/>" & _
"<employee name=""CDE"" age=""40"" sex=""male""/></department>"
Dim sr As New System.IO.StringReader(xmlString)
Dim doc As New Xml.XmlDocument
doc.Load(sr)
'or just in this case doc.LoadXML(xmlString)
Dim reader As New Xml.XmlNodeReader(doc)
While reader.Read()
Select Case reader.NodeType
Case Xml.XmlNodeType.Element
If reader.Name = "employee" Then
MessageBox.Show(reader.GetAttribute("name"))
End If
End Select
End While
///

I hope this helps,

Cor
 
Derek said:
For testing purposes, I need some code that will loop through all attributes
and elements in an XML file, and replace the data with the element or
attribute name? Does anybody have code that will do this, looping
generically through every node? VB.Net code would be appreciated so I can
see how it is done.

An XSLT stylesheet could do that. However your requirement is not clear,
what do you want to do with an element that contains other elements e.g.
<root>
<child>
<descendant>Kibology</descendant>
</child>
</root>
what should happen with the root and the child element do you want e.g.
<root>root
<child>child
<descendant>descendant</descendant>
</child>
</root>
or do you only want to output the element name of elements not having
any child elements e.g. for the example get
<root>
<child>
<descendant>descendant</descendant>
</child>
</root>
 
Back
Top