Home All Groups Group Topic Archive Search About
Author
19 Aug 2006 12:34 AM
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

Author
19 Aug 2006 2:25 AM
GhostInAK
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


Show quoteHide quote
> 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
>
Author
19 Aug 2006 4:41 AM
Derek Hart
Does anybody have code for this?


Show quoteHide quote
"GhostInAK" <ghosti***@gmail.com> wrote in message
news:c71747b42cd288c8910ad4e39fa4@news.microsoft.com...
> 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
>
>
>> 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
>>
>
>
Author
19 Aug 2006 5:26 AM
Cor Ligthert [MVP]
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


Show quoteHide quote
"Derek Hart" <derekmh***@yahoo.com> schreef in bericht
news:OI2yFn0wGHA.4680@TK2MSFTNGP04.phx.gbl...
> Does anybody have code for this?
>
>
> "GhostInAK" <ghosti***@gmail.com> wrote in message
> news:c71747b42cd288c8910ad4e39fa4@news.microsoft.com...
>> 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
>>
>>
>>> 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
>>>
>>
>>
>
>
Author
19 Aug 2006 12:49 PM
Martin Honnen
Derek Hart wrote:

> 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>


--

    Martin Honnen --- MVP XML
    http://JavaScript.FAQTs.com/