• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 99
  • Last Modified:

Help with entering "-" multiple location in NSC data element

Hi,

How do I convert data in my NSC data element from xxxxxxxxxxxxx to xxxx-xx-xxx-xxxx using VB.NET?

Thanks,

Victor
0
vcharles
Asked:
vcharles
  • 5
  • 4
1 Solution
 
Fernando SotoRetiredCommented:
Hi Victor;

What exactly is NSC data element? What is the data type of the field you are trying to re-format?
0
 
vcharlesAuthor Commented:
Hi Fernando,

I would like to convert file A to file B, for example if file A contains:

<Root>
<Table>
<ID>1</ID>>
<Item></Item>
<NSC>1111111111111<NSC>
</Table>
<Table>
<ID>2</ID>>
<Item></Item>
<NSC>2222222222222<NSC>
</Table>
<Table>
<ID>3</ID>>
<Item></Item>
<NSC>3333333333333<NSC>
</Table>
</Root>

How do I obtain file B as shown below:

<Root>
<Table>
<ID>1</ID>>
<Item></Item>
<NSC>1111-11-111-1111<NSC>
</Table>
<Table>
<ID>2</ID>>
<Item></Item>
<NSC>2222-22-222-2222<NSC>
</Table>
<Table>
<ID>3</ID>>
<Item></Item>
<NSC>3333-33-333-3333<NSC>
</Table>
</Root>

Victor
0
 
Fernando SotoRetiredCommented:
Hi Victor;

The following code snippet will reformat the NSC node as per example.

'' Load XML document into memory
Dim xdoc = XDocument.Load("C:\Working Directory\nscData.xml")                                               

// Get a reference to all the NSC nodes in the document                                                                                                            
Dim result = (From n In xdoc.Descendants("NSC")                                                             
              Select n).ToList()                                                                            

'' Reformat the Text in NSC node                                                                                                             
result.ForEach(Sub(nsc) nsc.Value = Regex.Replace(nsc.Value, "(\d{4})(\d{2})(\d{3})(\d{4})", "$1-$2-$3-$4"))

xdoc.Save("C:\Working Directory\nscData-Reformatted.xml")

Open in new window

0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
vcharlesAuthor Commented:
Thank You.
0
 
vcharlesAuthor Commented:
Thank You.
0
 
Fernando SotoRetiredCommented:
You will need this import for the regular expression.

Imports System.Text.RegularExpressions
0
 
Fernando SotoRetiredCommented:
Hi Victor, how are you doing with this question?
0
 
vcharlesAuthor Commented:
Hi Fernando,

I am sorry for the late reply. The codes worked.

Thank You.
0
 
Fernando SotoRetiredCommented:
Not a problem Victor, glad to help.
0
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.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now