Help with copying partial data from data element 1 to dataelement 2 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 the word "to" in <NSC> to <RN>, if NSC contains +55 to -20 I want to copy -20 to <RN> and delete "to -20" in <NSC>. The xml file contains additional data elements not shown in my example.

 Part A

 <Root>
 <Table>
 <ID>1</ID>
 <NSC>+55 to -20</NSC>
 <RN><RN>
 </Table>
 <Table>
 <ID>2</ID>
 <NSC>+100 to -70</NSC>
 <RN><RN>
 </Table>
 </Root>

 Part B:

 <Root>
 <Table>
 <ID>1</ID>
 <NSC>+55</NSC>
 <RN>-20</RN>
 </Table>
 <Table>
 <ID>2</ID>
 <NSC>+100</NSC>
 <RN>-70</RN>
 </Table>
 </Root>

 Thanks,

 Victor
vcharlesAsked:
Who is Participating?
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.

Fernando SotoRetiredCommented:
Hi Victor;

This should work.
'' Load the XML document into memory
Dim xdoc = XDocument.Load("C:\Working Directory\Victor.xml")

'' Get a List of all the NSC and RN node elements
Dim results = (From n In xdoc.Descendants("NSC")
               Select NSC = n, RN = n.Parent.Element("RN")).ToList()

'' Get the values from the NSC node and re-write the valies in NSC an RN
For Each nscRn In results
    Dim nscValue As String = nscRn.NSC.Value.Trim()
    If Not Regex.Match(nscValue, "^[+-]?\d+$").Success Then
        Dim nscRnMatches As MatchCollection = Regex.Matches(nscValue, "([+-]?\d+)")
        Dim nscV = nscRnMatches.Item(0).Value
        Dim rnV = nscRnMatches.Item(1).Value
        nscRn.NSC.Value = nscV
        nscRn.RN.Value = rnV
    End If
Next

xdoc.Save("Path and filename here")

Open in new window

0

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:
Thank you, I will try it and get back to you.
0
vcharlesAuthor Commented:
Hi Fernando,

I'm getting error message: Specified argument was out of the range of valid values, parameter name:i

Not all the records have values in NSC not certain if that is waht is causing the error. Also the NSC contain: <NSC>  -40 C  To  +50 C</NSC> and sometimes <NSC>-99 C  To  +99 C</NSC>, the spacing varies by records, not certain if better to remove the spaces before executing the code.

Thanks,

Victor
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

vcharlesAuthor Commented:
Hi again,

When I include "On error resume next" the code works but the :degrees celcius symbols in front of the numbers are missing.
0
vcharlesAuthor Commented:
Hi again,

please disregard last message, will just add Degrees-Celcius in the column header.

Thanks,

Victor
0
vcharlesAuthor Commented:
But it still doesn't work, without on error resume next, could it be because some of the fields are blank.
0
Fernando SotoRetiredCommented:
It is hard to say without seeing the actual XML file/s that is causing the error.
0
vcharlesAuthor Commented:
Thank You.
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.