Cranialsurge
Cranialsurge

Reputation: 245

How to remove whitespaces from XML file

I have the following XML file. How do I remove the unnecessary whitespaces from it? I tried using XDocument.Save and removing formatting, but it didn't work. I got an exception while trying to parse the file. Essentially I want to normalize the text so that it doesn't contain those whitespaces. Should I resort to Regex?

< ? x m l v e r s i o n = " 1 . 0 " e n c o d i n g = " u t f - 1 6 " ? >

 < n s 0 : C D I S c h e m a E r r o r   x m l n s : n s 0 = " h t t p : / / M i c r o s o f t . I T . R e l a t i o n s h i p M a n a g e m e n t . S c h e m a s . P a r t n e r F u n d S u m m a r y S c h e m a E r r o r / V 1 " > 

     < E r r o r I n f o r m a t i o n > T h e r e   w a s   a   f a i l u r e   e x e c u t i n g   t h e   r e c e i v e   p i p e l i n e :   " s d f g t w . I T . R e l a t i o n s h i p M a n a g e m e n t . P i p e l i n e s . N o n C a s h P a y m e n t R e s p o n s e V 1 C S V T o X m l F i l e C o n v e r s i o n ,   t b f a e . I T . R e l a t i o n s h i p M a n a g e m e n t . P i p e l i n e s   U R I :   " D : \ t e s t u r l f o l d e r \ S u b S y s t e m \ S t a g i n g \ V I R P \ * . c s v "   R e a s o n :   T h e   d o c u m e n t   f a i l e d   t o   v a l i d a t e   b e c a u s e   o f   t h e   f o l l o w i n g   e r r o r : " T h e   ' R e f e r e n c e N u m b e r '   e l e m e n t   h a s   a n   i n v a l i d   v a l u e   a c c o r d i n g   t o   i t s   d a t a   t y p e . "   .     < / E r r o r I n f o r m a t i o n > 
     < O r i g i n a l M e s s a g e >  > Id,StatusCode,ReferenceNumber,DateTime,ErrorMessage

41977,0,12344,2011-08-29T15:16:06.120,blackety blah < / O r i g i n a l M e s s a g e > < / n s 0 : C D I S c h e m a E r r o r >

Upvotes: 1

Views: 861

Answers (1)

Alexei Levenkov
Alexei Levenkov

Reputation: 100527

It looks like you are reading UTF-16 document by using Utf8 or ASCII encoding. Open stream correctly and you should not see strange characters (which should be \0, not spaces).

Sample (by Dmytro Tsiniavsky):

XElement body; 
using(var sReader = new StreamReader(fileName,Encoding.Unicode))
{
  body = XDocument.Parse(sReader.ReadToEnd());
}

Upvotes: 4

Related Questions