Which is the very thing I'd want to prevent.
Really? It isn't generally important at all whether that escaping is applied; the XML infoset for either is the same.
I am frankly a bit surprised that the document loads at all.
>
is a perfectly valid character to include in an attribute value. The only place >
may need to be &
-escaped in XML is in a ]]>
sequence in text content, due to an obscure and silly rule in the spec.
To avoid having to think about the problem, many XML serialisers habitually escape >
anywhere in text content or attribute values.
The Canonical XML specification specifies one particular way of serialising an XML document so the output can be compared as a simple string; for example it states exactly how attributes should be ordered. Canonical XML endorses >
-escaping in text content, but it denies it in attribute values. So if you used a Canonical XML serialiser to output your document you'd get the result you expected for that particular value. (I can't guarantee it'd look how you want for other examples though.)
You can get a canonicaliser in .NET using XmlDsigC14NTransform (or maybe XmlDsigC14NWithCommentsTransform), something like:
XmlDsigC14NTransform transform= new XmlDsigC14NTransform(false);
transform.LoadInput(doc);
Stream stream= (Stream) t.GetOutput(typeof(Stream));
// write stream to file