Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2007-12-05
7
Medium Priority
?
1,859 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 Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

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.
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

604 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