Solved

Help with converting values using VB.NET

Posted on 2013-11-09
2
332 Views
Last Modified: 2013-11-10
Hi,

I'm using the code below to convert data in xml file in Part A format.
How do I modify the code to work when Receiver_ID has multiple values? For example if
Receiver_ID is 1,2, I need to transfer 1,2 to BEL,CAN  since in the country's xml file 1 pertains to BEL and 2 pertains to CAN.

Code:

  Dim dt2 As New DataTable("Country")
  dt2.Columns.Add("Link_ID")
  dt2.Columns.Add("Receiver")

  Dim linker2 As XElement = XElement.Load(Application.StartupPath + "\LinkFiles\LinkCountry" & Trim(LoginForm1.username.Text) & ".xml")

 Dim receiver2 As XElement = XElement.Load(Application.StartupPath + "\Receiver.xml")
       

For Each item As XElement In linker2.Elements("Country")
            Dim linkID As String = item.Element("Link_ID").Value
            Dim receiverId As String = item.Element("Receiver_ID").Value

            Dim receiverVal As String = String.Empty
            xe = receiver2.Elements("Row").Cast(Of XElement)().Where(Function(n) n.Element("Receiver_ID").Value = receiverId).FirstOrDefault()
            If xe IsNot Nothing Then
                receiverVal = xe.Element("Receiver").Value
            End If
   Dim dr As DataRow = dt2.NewRow()
            dr("Link_ID") = linkID
            dr("Receiver") = receiverVal
            dt2.Rows.Add(dr)
        Next

        dt2.WriteXml(Application.StartupPath + "\LinkFiles\LinkCountryMain" & Trim(LoginForm1.username.Text) & ".xml")
        '***

Part A

<?xml version="1.0" standalone="yes"?>
<DocumentElement>
  <Country>
    <Link_ID>1</Link_ID>
    <Receiver_ID>1</Receiver_ID>
    <C_ID>1</C_ID>
  </Country>
  <Country>
    <Link_ID>2</Link_ID>
    <Receiver_ID>2</Receiver_ID>
    <C_ID>2</C_ID>
  </Country>
</DocumentElement>

Part B

<?xml version="1.0" standalone="yes"?>
<DocumentElement>
  <Country>
    <Link_ID>1</Link_ID>
    <Receiver_ID>1,2</Receiver_ID>
    <C_ID>1</C_ID>
  </Country>
  <Country>
    <Link_ID>2</Link_ID>
    <Receiver_ID>2</Receiver_ID>
    <C_ID>2</C_ID>
  </Country>
  </DocumentElement>

Thanks,

Victor
0
Comment
Question by:vcharles
2 Comments
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 500 total points
ID: 39636684
Try this:
        For Each item As XElement In linker2.Elements("Country")
            Dim linkID As String = item.Element("Link_ID").Value

            Dim receiverVal As String = String.Empty
            For Each receiverIdSplit As String In item.Element("Receiver_ID").Value.Split({","c})
                Dim receiverId As String = receiverIdSplit
                xe = receiver2.Elements("Row").Cast(Of XElement)().Where(Function(n) n.Element("Receiver_ID").Value = receiverId).FirstOrDefault()
                If xe IsNot Nothing Then
                    If Not receiverVal.Equals(String.Empty) Then receiverVal &= ","
                    receiverVal &= xe.Element("Receiver").Value
                End If
            Next
            Dim dr As DataRow = dt2.NewRow()
            dr("Link_ID") = linkID
            dr("Receiver") = receiverVal
            dt2.Rows.Add(dr)
        Next

Open in new window

0
 

Author Closing Comment

by:vcharles
ID: 39637306
Thank You.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
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 …
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

747 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

13 Experts available now in Live!

Get 1:1 Help Now