Solved

Help with converting values using VB.NET

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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…

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