Solved

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

Posted on 2007-12-05
7
1,846 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 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

726 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