001
001

Reputation: 65145

how to remove carriage returns, newlines, spaces from a string

How do I remove the spaces, carriage returns from in between < and > ?

**START BELOW THIS LINE***

<TestItem1Request>
  <Username>admin</Username>
  <Password>123abc..@!</Password>
  <Item1>this is an item</Item1>
</TestItem1Request>

**END HERE **

I want the string result to end up as the following...

</TestItem1Request><Username>admin</Username><Password>123abc..@!</Password><Item1>this is an item</Item1></TestItem1Request>

How do I do this?

Upvotes: 29

Views: 19929

Answers (1)

Ahmad Mageed
Ahmad Mageed

Reputation: 96487

If this is valid XML you could use the SaveOptions.DisableFormatting enumeration:

string input = @"<TestItem1Request>
  <Username>admin</Username>
  <Password>123abc..@!</Password>
  <Item1>this is an item</Item1>
</TestItem1Request>";

string result = XElement.Parse(input).ToString(SaveOptions.DisableFormatting);
Console.WriteLine(result);

Upvotes: 63

Related Questions