asp.net consultancy chorley ASP.NET Server Side Scripting Wigan
Website Development Radcliffe

IT Services And Support

Email : ren@techsolus.co.uk
Mobile Phone : 0788 68 41 411
Answerphone : 01204 469683
bespoke invoice systems Standish Information Technology Advice Worsley
    development systems Ramsbottom
data manipulation Farnworth access databases Whitefield SQL connections Atherton
online accessible software Leyland software compatible Coppull


Get connected and Online Tottington
remote management Makerfield code and design Aspull

ASP.NET XML White Spaces and Carriage Returns - 22/10/2010

It's amazing what you learn.

In XML files, the white spaces and carriage return DO MATTER


So, there I am trying to work out some data from an XML file.  I actually was trying to use an XML file to contain swear words and the text I would use to replace the said swear word.  It took a little while but I worked out how to loop through the XML Datatable that is created when you read the XML.  But even after I learnt how to extract the swear word and it's replacement text, it did not work.

I figured out the issue was the XML words seemed to come with white space and carraige returns.  Why...here's my XML

<swearwords>

<word>
 <badword>
  pooh
 </badword>
 <goodword>
  p##h
 </goodword>
</word>

<word>
 <badword>
  pants
 </badword>
 <goodword>
  p##ts
 </goodword>
</word>

</swearwords>


Now, you'll note, being a programmer who writes html, ASP.NET, Javascript and  a little PHP by hand, using notepad, I've become used to spacing things out and indenting to make re-reading my code as easy as possible.  I HATE programming software...but that's another story.  However the hand-written XML file did cause me the issue.  You'll note the carriacge return before and after each item, and the indent spaces too.  It seems ASP.NET's XML reader picks these up too.  Initally I was annoyed but on reflection it makes sense, perhaps you may want the XML file to store carriage returns and white spaces.  I'm just so used to good old HTML igoniring all this to the most part.

The solution...

<swearwords>

<word>
 <badword>pooh</badword>
 <goodword>p##h</goodword>
</word>

<word>
 <badword>pants</badword>
 <goodword>p##ts</goodword>
</word>

</swearwords>


ASP.NET's XML reader only collects the carriage returns and white spaces INSIDE the elements.  The rest of the layout is up to me, just don't put them INTO the element.

It's sooooo easy when you know how.

Post A Comment

Name Comment
programming services asp.net specialist
Valid XHTML 1.0 Transitional
Admin
GD