?
Solved

how can i traverse and edit each xml node data using vb.net

Posted on 2007-12-05
7
Medium Priority
?
1,851 Views
Last Modified: 2012-06-27
Hi

Is there any code in vb.net that traverse through each and every node allow us to change on any specific. I need to traverse each node if exist and their child nodes if exist and need to modify that specific node content. e.g. US to United States
Xml sample:

 <GATE>
  <GAT>Q</GAT>
  <GA>Mon</GA>
    <DEST>
     <GR>
       <GC>15</GC>
       <GN>All</GN>
      </GR>
      <GR>
        <GC>15</GC>
        <GN>Cuba</GN>
          <DES>
            <DE>
                <DC>15</DC>
                <DN>US</DN>
            </DE>
          </DES>
      </GR>
    </DEST>
 </GATE>

Regardless of how deep the xml is ..i need to traverse each node and its child node and so on ... any vb.net code sample would help me

Thanks
Shaukat

0
Comment
Question by:shwaqar82
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
7 Comments
 
LVL 38

Accepted Solution

by:
PaulHews earned 1000 total points
ID: 20414055
Something like this should work:

Imports System.Xml
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim xmlDoc As New XmlDocument
        xmlDoc.Load("C:\temp\test.xml")  'To your xml file or url... if its a string, use  xmldoc.LoadXml

        TraverseFixNodes(xmlDoc.DocumentElement)

        xmlDoc.Save("C:\temp\new.xml")  'Save fixed file

    End Sub

    'recursive sub to traverse and change text of nodes
    Private Sub TraverseFixNodes(ByVal node As XmlNode)
        If node.InnerText = "US" Then node.InnerText = "United States"
        For Each childNode As XmlNode In node.ChildNodes
            TraverseFixNodes(childNode)
        Next
    End Sub

End Class
0
 
LVL 60

Assisted Solution

by:Geert Bormans
Geert Bormans earned 1000 total points
ID: 20414078
I would use an XSLT

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:template match="*">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates select="node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="text()">
        <xsl:choose>
            <xsl:when test=". = 'US'">United States</xsl:when>
            <xsl:otherwise><xsl:value-of select="."/></xsl:otherwise>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>

cheers

Geert
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 20414094
One other quickie method... If you have the XML in a string variable, you can use the string methods:

xmlString = xmlString.Replace("US", "United States")
0
Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

 
LVL 38

Expert Comment

by:PaulHews
ID: 20414110
But that could be dangerous if the text searched for is in the tags of the nodes and not in the data.... I would not use this unless I knew exactly what the possibilities were.
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 20414116
> xmlString = xmlString.Replace("US", "United States")

make it xmlString = xmlString.Replace(">US<", ">United States<")
to be sure it is only in element content
0
 
LVL 1

Expert Comment

by:Computer101
ID: 20632934
Forced accept.

Computer101
EE Admin
0

Featured Post

Python: Series & Data Frames With Pandas

Learn the basics of Python’s pandas library of series & data frames and how we can use these tools for data manipulation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question