Solved

Help with converting values using VB.NET

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
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…

738 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