Help with removing space and "/" from dataelements using VB.NET

Hi,

If File 1 contains:

<Root>
<table>
<NSC>C 28<NSC>
<AGD>H /77<AGD>
<NSN>C/78<NSN>
<UAD>D   56<UAD>
</table>
</Root>

How do I obtain remove the spaces and "/" to obtain:

<Root>
<table>
<NSC>C28<NSC>
<AGD>H77<AGD>
<NSN>C78<NSN>
<UAD>D56<UAD>
</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.

MlandaTCommented:
Dim myXmlDocument as XmlDocument = new XmlDocument()
'myXmlDocument.Load ("data.xml")
myXmlDocument.LoadXml ("<Root><table><NSC>C 28</NSC> <AGD>H /77</AGD> <NSN>C/78</NSN> <UAD>D   56</UAD> </table></Root>")

For Each node In myXmlDocument.DocumentElement.ChildNodes(0).ChildNodes

	node.InnerText = node.InnerText.Replace(" ","").Replace("/", "")

Next

'myXmlDocument.Save("data1.xml")
myXmlDocument.Dump

Open in new window

Jaime OlivaresSoftware ArchitectCommented:
Extending the answer from MlandaT, you can also use regex:
node.InnerText = Regex.Replace(node.InnerText, " /", "" )
Fernando SotoRetiredCommented:
Hi Victor;

This should do what you need.
'' Load the XML into memory
Dim xdoc = XDocument.Load("C:\Working Directory\VictorFile3.xml")
'' Get a reference to all the Child elements of each table node
Dim results = (From n In xdoc.Descendants("table").Elements()
               Select n).ToList()
'' Remove unwanted characters
results.ForEach(Sub(n) n.Value = Regex.Replace(n.Value,"[ /]+", ""))

'' Save the modified XML to file system
xdoc.Save("Path and filename goes here")

Open in new window

Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

vcharlesAuthor Commented:
Hi,

 How do I modify the code to only apply to one data element (i.e. NSC)

Thank you.

Victor
MlandaTCommented:
Use the SelectNodes method (http://www.csharp-examples.net/xml-nodes-by-name/)
Dim myXmlDocument as XmlDocument = new XmlDocument()
'myXmlDocument.Load ("data.xml")
myXmlDocument.LoadXml ("<Root><table><NSC>C 28</NSC> <AGD>H /77</AGD> <NSN>C/78</NSN> <UAD>D   56</UAD> </table></Root>")

For Each node In myXmlDocument.DocumentElement.ChildNodes(0).SelectNodes("NSC")

	node.InnerText = node.InnerText.Replace(" ","").Replace("/", "")

Next

'myXmlDocument.Save("data1.xml")
myXmlDocument.Dump

Open in new window

Fernando SotoRetiredCommented:
Hi Victor;

Here you go.
'' Load the XML into memory
Dim xdoc = XDocument.Load("C:\Working Directory\VictorFile3.xml")
'' Get a reference to all the Child elements of each table node
Dim results = (From n In xdoc.Descendants("table").Elements()
               Where n.Name.LocalName = "NSC"
               Select n).ToList()
'' Remove unwanted characters
results.ForEach(Sub(n) n.Value = Regex.Replace(n.Value,"[ /]+", ""))

'' Save the modified XML to file system
xdoc.Save("Path and filename goes here")

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:
Thank You, will try it and get back to you.
vcharlesAuthor Commented:
Thank You.
Fernando SotoRetiredCommented:
Not a problem, glad to help.
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.