[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Help with converting values using VB.NET

Posted on 2013-11-09
2
Medium Priority
?
349 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 2000 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

650 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