|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Replace All XML DataFor 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 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 > 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 >> > > 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 >>> >> >> > > Derek Hart wrote:
> For testing purposes, I need some code that will loop through all attributes An XSLT stylesheet could do that. However your requirement is not clear, > 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. 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>
Late Binding Issue
string reset? why? how? Decimal Degrees to DMS Need some advice? What is the .NET way? - Right("0" & Now.Month.ToString, 2) Very large string HTTPS And VB.Net Apps Concurrency violation: the DeleteCommand affected 0 records vb.net Web Service using stored procedures example? Basic multithreading problem |
|||||||||||||||||||||||