Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Help with removing blank space in data element using VB.NET

Posted on 2014-08-04
4
Medium Priority
?
252 Views
Last Modified: 2014-08-05
Hi,


How do I delete blank spaces at the beginning of data element and between commas? For example if my xml file contains:

<Root>
<Table1>
<Item> BEL</Item>
</Table1>
<Table1>
<Item>BEL,   CAN</Item>
</Table1>
<Table1>
<Item>   BEL,    CAN,      USA</Item>
</Table1>
<./Root>

Desired results:

<Root>
<Table1>
<Item>BEL</Item>
</Table1>
<Table1>
<Item>BEL,CAN</Item>
</Table1>
<Table1>
<Item>BEL,CAN,USA</Item>
</Table1>
<./Root>

Thanks,

Victor
0
Comment
Question by:vcharles
  • 2
4 Comments
 
LVL 37

Expert Comment

by:Kimputer
ID: 40240055
If you are sure ALL lines REALLY do not require spaces, pass them all through:

myString = myString.Replace(" ", "")

Open in new window


If some do need spaces, only pass those that need to be removed through the code (for instance, if line nr = xx, or if mystring.contains("tag") then)
0
 
LVL 64

Accepted Solution

by:
Fernando Soto earned 2000 total points
ID: 40241262
Hi Victor;

The code snippet below will remove all the spaces in the Item nodes in the XML document.

' Load document
Dim xdoc = XDocument.Load("... .xml")

Dim results As List(Of XElement) = (From n In xdoc.Descendants("Item") _
                                    select n).ToList()

' Remove the spaces               
results.ForEach(Sub(n) n.Value = n.Value.Replace(" ", String.Empty))  
' Save the document
xdoc.Save(... .xml")

Open in new window

0
 

Author Closing Comment

by:vcharles
ID: 40241360
Thank You!
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 40241442
No a problem Victor; glad to help.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question