Help with copying partial data from Data Element A to Dta Element B using VB.NET

Hello,

I have an xml file as shown if Part A, how do I obtain xml file in Part B, for example I want to copy all text after 16 characters in <NSC> to <RN>, if NSC contains xxxx-xx-xxx-xxxx(1) or NSC contains xxxx-xx-xxx-xxxx(1, 2 ) I want to copy (1) or (1,2) to <RN>. The xml file contains additional data elements not shown in my example.

Part A

<Root>
<Table>
<ID>1</ID>
<NSC>xxxx-xx-xxx-xxxx(1)</NSC>
<RN><RN>
</Table>
<Table>
<ID>2</ID>
<NSC>xxxx-xx-xxx-xxxx(1,2)</NSC>
<RN><RN>
</Table>
<Table>
<ID>3</ID>
<NSC>xxxx-xx-xxx-xxxx(1,2.3)</NSC>
<RN><RN>
</Table>
</Root>

Part B:

<Root>
<Table>
<ID>1</ID>
<NSC>xxxx-xx-xxx-xxxx</NSC>
<RN>(1)</RN>
</Table>
<Table>
<ID>2</ID>
<NSC>xxxx-xx-xxx-xxxx</NSC>
<RN>(1,2)</RN>
</Table>
<Table>
<ID>3</ID>
<NSC>xxxx-xx-xxx-xxxx</NSC>
<RN>(1,2.3)</RN>
</Table>
</Root>

Thanks,

Victor
vcharlesAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Robert SchuttSoftware EngineerCommented:
How about this:
    Private Sub DoTransform()
        Const C_CUTOFF_LENGTH As Integer = 16

        Dim objFileA As New XmlDocument
        objFileA.Load("FileA.xml")
        For Each objTable As XmlElement In objFileA.DocumentElement.SelectNodes("Table")
            Dim objNodeNSC As XmlElement
            objNodeNSC = objTable.SelectSingleNode("NSC")
            If objNodeNSC IsNot Nothing AndAlso objNodeNSC.InnerText.Length > C_CUTOFF_LENGTH Then
                Dim objNodeRN As XmlElement
                objNodeRN = objTable.SelectSingleNode("RN")
                If objNodeRN Is Nothing Then
                    objNodeRN = objTable.AppendChild(objFileA.CreateElement("RN"))
                End If
                objNodeRN.InnerText = objNodeNSC.InnerText.Substring(C_CUTOFF_LENGTH)
                objNodeNSC.InnerText = objNodeNSC.InnerText.Substring(0, C_CUTOFF_LENGTH)
            End If
        Next
        objFileA.Save("FileB.xml")
    End Sub

Open in new window

Fernando SotoRetiredCommented:
Hi Victor;

The following code snippet using Linq to XML should give you what you are looking for. I am assuming you what the resulting XML file to be the same as the original with the changes stated.

Dim partA As XDocument = XDocument.Load("C:\Working Directory\partA.xml")
                                                                         
Dim nodes = (From node In partA.Descendants("Table")                     
             Where node.Element("NSC").Value.Length > 16                 
             Select                                                      
                NSC = node.Element("NSC"),                               
                RNValue = node.Element("NSC").Value.Substring(16),       
                RN = node.Element("RN")).ToList()                        
                                                                         
For Each node In nodes                                                   
    node.NSC.Value = node.NSC.Value.Substring(0, 15)                     
    node.RN.Value = node.RNValue                                         
Next                                                                     
                                                                         
partA.Save("C\Working Directory\partB.xml")                              

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
vcharlesAuthor Commented:
Hello Fernando, Robert;

Thank you for the solutions.

Victor
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.