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?
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

0
Jaime OlivaresSoftware ArchitectCommented:
Extending the answer from MlandaT, you can also use regex:
node.InnerText = Regex.Replace(node.InnerText, " /", "" )
0
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

0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

vcharlesAuthor Commented:
Hi,

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

Thank you.

Victor
0
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

0
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

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