I work with some xml files which have UTF-8 characters like ©, ë
but they are converted to their image counterparts i.e. ©, ë, whenever I use XDocument to modify the file and also the self-closing nodes e.x. <a type="box"/>
become <a type="box" />
i.e. an extra whitespace is added to the node, but I want to keep the entities like ©, ë etc as it is and also remove the extra whitespace in the self-closing nodes.
Currently I’m doing it in below fashion (sample example code)
var path = @"C:\Users\Temp\sample.xml"; var content=File.ReadAllText(path); //replace all "&" with "&" to prevent conversion of entity var newcontent=content.Replace("&", "&"); File.WriteAllText(path,newcontent); XDocument doc = XDocument.Load(path, LoadOptions.PreserveWhitespace); string _count = doc.Descendants("fig").Count().ToString(); doc.Descendants("fig-count").First().Value = _count; doc.Save(path, SaveOptions.DisableFormatting); //replace all "&" with "&" to go back to the original state and also replace all whitespace //in the self-closing nodes after the XDocument process complets var x=File.ReadAllText(path); var y=x.Replace(" />", "/>"); File.WriteAllText(path,y.Replace("&", "&"));
Can anyone show me a more efficient way of doing this?