How to edit XML in C # without changing format / spacing?

I need an application that goes through an XML file, changes some attribute values ​​and adds other attributes. I know I can do this with XmlDocument and XmlWriter. However, I don't want to change the spacing of the document. Is there any way to do this? Or, should I parse the file myself?

+2


source to share


2 answers


XmlDocument

has a property PreserveWhitespace

. If you set a value true

, minor whitespace will be retained.

See MSDN

EDIT

If I execute the following code, spaces, including line breaks, are preserved. (It is true that the space is inserted between <b

and />

)



    XmlDocument doc = new XmlDocument();
    doc.PreserveWhitespace = true;
    doc.LoadXml(
@"<a>
   <b/>
</a>");
    Console.WriteLine(doc.InnerXml);

      

Output:

<a>
   <b />
</a>

      

+3


source


Minor spaces are usually discarded or reformatted. Therefore, if the XML file does not use an attribute xml:space="preserve"

on the nodes that must preserve their exact spaces, changing the space is in order on the XML specification.



0


source







All Articles