Xml replace method removes indentations

  • Thread starter Thread starter Amendra
  • Start date Start date
A

Amendra

Hi,

I am using the document.replacechild method to replace some values. But when
used, it removes the previous indentation that was in the document for that
node. Actually looks like the CRLF is being removed for some reason, any
luck.

Eg...

The original doc
<locationAdd>

<friendlyName>

<ColumnNo>2</ColumnNo>

</friendlyName>

<ColumnNo>2</ColumnNo>

<currencyCode>

<ColumnNo>2</ColumnNo>

< /currencyCode>

</locationAdd>



The replaced doc...

<locationAdd>

<friendlyName>will</friendlyName>will<currencyCode>will</currencyCode></loca
tionAdd>
 
It will do, the XmlDom has no concept of spacing, carriage returns or the
like. These are all this that can exist in a .xml file for readability when
we edit them, and when IE views the xml file, it ignores your spacing and
carriage returns and presents the XML in a predifined layout.

I think that what is happening is when you pass your document to the dom,
all the nodes are read out, your replacment occurs, then you convert it back
to a document that contains no spacing...

hope that helps.
Dan.

Hi,

I am using the document.replacechild method to replace some values. But when
used, it removes the previous indentation that was in the document for that
node. Actually looks like the CRLF is being removed for some reason, any
luck.

Eg...

The original doc
<locationAdd>

<friendlyName>

<ColumnNo>2</ColumnNo>

</friendlyName>

<ColumnNo>2</ColumnNo>

<currencyCode>

<ColumnNo>2</ColumnNo>

< /currencyCode>

</locationAdd>



The replaced doc...

<locationAdd>

<friendlyName>will</friendlyName>will<currencyCode>will</currencyCode></loca
tionAdd>
 
Hello!

This came up a week ago. You can use the following method to format the
string of the Xml. Please take notice, that you are able to pass the
XmlDocument (or XmlNode) to the XmlTextWriter (instead of the string as in
this example).

These lines of code will pretty-print your Xml string ..

XmlTextReader r = new XmlTextReader(new StringReader(xmlString));
r.MoveToContent();
StringWriter sw = new StringWriter();
XmlTextWriter w = new XmlTextWriter(sw);
w.Formatting = Formatting.Indented;
w.WriteNode(r,false);
w.Close();
return sw.ToString();
 
Back
Top