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?
source to share
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>
source to share