Solved

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

Posted on 2007-12-05
7
1,842 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
  • 3
  • 2
7 Comments
 
LVL 38

Accepted Solution

by:
PaulHews earned 250 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 250 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

813 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now