Solved

Help with converting values using VB.NET

Posted on 2013-11-09
2
338 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

776 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