views:

1462

answers:

4

How can I remove whitespace on every instance of a particular node which I specify in C#? For example let's say that I have the following XML document:

<XML_Doc>

  <Record_1>
     <Name>Bob</Name>
     <ID_Number>12345</ID_Number>
     <Sample>
     </Sample>
  </Record_1>

  <Record_2>
     <Name>John</Name>
     <ID_Number>54321</ID_Number>
     <Sample>
     </Sample>
  </Record_2>

</XML_Doc>

What I would like is to take every instance of the <Sample> tag and change the formatting so it looks like this:

<XML_Doc>

  <Record_1>
     <Name>Bob</Name>
     <ID_Number>12345</ID_Number>
     <Sample></Sample>
  </Record_1>

  <Record_2>
     <Name>John</Name>
     <ID_Number>54321</ID_Number>
     <Sample></Sample>
  </Record_2>

</XML_Doc>

EDIT:

The other application which makes use of the XML file was not written by me and I cannot modify the structure of the XML document itself. I am writing a utility which parses the XML file and replaces each instance of the node I specify. The example where the <Sample></Sample> tag is on the same line is how the document is originally formatted and how I need it to be for the other application to be able to read it correctly.

A: 

If you're interested in preserving whitespace (ie: tabs, carriage returns and other formats) you can use the CDATA (unparsed character data).

<![CDATA[
]]>

However, if you just want to have an XML document that is formatted a certain way for aesthetic purposes , I would advise you to leave it alone.

To write a CDATA section into an XML Document use the following code:

XmlNode itemDescription = doc.CreateElement("description");
XmlCDataSection cdata = doc.CreateCDataSection("<P>hello world</P>");
itemDescription.AppendChild(cdata);
item.AppendChild(itemDescription);

This produces

<description><![CDATA[<P>hello world</P>]]></description>
Oplopanax
A: 

Based on your comments, I think you may be able to use the XmlWriterSettings.NewLineHandling property to change how the text node handles the carriage returns. You'll have to experiment. I suspect the receiving application has some line parsing issue and is looking for a \r\n or something.

Oplopanax
A: 

I think you want to set

xml.Settings.NewLineHandling = NewLineHandling.Entitize;
esac
A: 

I would suggest using an xslt transform. Doing it this way allows you to have control over the stripping or preservation of white space.

MSDN has an article that addresses this issue, see: Controlling White Space with the DOM

Al