tags:

views:

168

answers:

2

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, will I have to parse the file myself?

A: 

Insignificant whitespace will typically be thrown away or reformatted. So unless the XML file uses the xml:space="preserve" attribute on the nodes which shall preserve their exact whitespace, changing whitespace is OK per XML specifications.

Lucero
Thanks. I put the xml:space="preserve" attribute on the root node and it's keeping most of the spacing. However, for nodes that span multiple lines, it's putting them on one line. And, I've noticed for simple tags that end in />, it is inserting a space before the end tag. For instance, for the node "<UniformGrid Rows="1"/>", it changes it to "<UniformGrid Rows="1" />". That's not a big deal but the multiple lines is. Is there a way to preserve the multiple lines? The reason I care is so we can easily compare changes in source control (since I am actually modifying XAML).
bsh152s
No, that's not going to work with the normal XML classes, because the whitespace inside the node definition is not part of the document whitespace. The xml:space makes sure that all content whitespace in the document is kept unchanged, because whitespace such as the one between two tags with no text in between is also not significant any may be changed (in fact, this is what allows you to nicely indent documents without changing their meaning).
Lucero
+3  A: 

XmlDocument has a property PreserveWhitespace. If you set this to true insignificant whitespace will be preserved.

See MSDN

EDIT

If I execute the following code, whitespace including line breaks is preserved. (It's true that a space is inserted between <b and />)

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

The output is:

<a>
   <b />
</a>
Peter van der Heijden
That doesn't preserve all white space. White space inside elements (ie. in the element itself, not its body) is not preserved - `<something/>` will get converted to `<something />`.
adrianbanks
Thanks. That does the same as the xml:space="preserve" attribute and is much cleaner. But it still doesn't preserve line breaks. Is there a way to preserve breaks also?
bsh152s
@Brandon - As far as I can see line breaks are preserved. I have added a code sample.
Peter van der Heijden
Peter van der Heijden--Your example doesn't include a node that has attributes on different lines (see example below). According to Lucero, this isn't possible. If you disagree, I'm all ears.<a attrib1="1" attrib2="2"> <b /></a>
bsh152s
Sorry, I did not see that that was the requirement. I don't think there is a way to do that apart from custom parsing.
Peter van der Heijden