Linq to XML 正在减小文件大小

Linq to XML is Reducing File Size

我有一个 XML 文件,大小为 1,030KB。在我 运行 我的代码(将一个元素添加到 xml 文件的一部分)之后,文件大小减小到 580kb。我认为这可能是读取此文件的辅助应用程序无法读取新生成的文件的原因。我不确定为什么会这样。有没有办法阻止 Linq to XML 压缩文件?

XDocument xDoc = XDocument.Load(cust_file);

XElement parentXElement = xDoc.XPathSelectElement("LastLayout2/CommandBars");
XElement refXElement = xDoc.XPathSelectElement("LastLayout2/CommandBars/CommandBar[@Title = 'Standard']/Controls");

XElement temp = refXElement.XPathSelectElement("Control[@Parameter = 'GLOBAL!QMS_Launcher.Main']");

if (temp == null)
{
XElement newElement = new XElement("Control");
XAttribute classAt = new XAttribute("Class", "CXTPControlButton");
XAttribute idAt = new XAttribute("Id", "0");
XAttribute paramAt = new XAttribute("Parameter", "GLOBAL!QMS_Launcher.Main");
XAttribute custIdAt = new XAttribute("CustomIconId", "68267");

XElement customIcon = new XElement("CustomIcon");
XElement icon = new XElement("Icon");
XAttribute width = new XAttribute("Width", "16");
XAttribute data = new XAttribute("Data", "ABAAAAAAFCAAAAAAEDAAICAAAAAAABAAAAAAABAAAAAABAAAACAADAAAAAAAAAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAPPAAAAPPAAAAPPAAAAAAAAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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EAAKGAAAAAAAAAAAAAAAAAAAAAANJAAAAAAPPAAAAAANMAAAAAABCAAAAAANBAAAAAABIAAAAAAPPAAAAAAHOAEKCAAMEIHOEAAIHIHOEAAIHIHOEAAIHIHOEAAIHJGDEAAJGGCJBAAGCAAAAAAAAAAAAAAFBAAAAAADOAAAAAAPPAAAAAAPPAAAAAAPPAAAAAAPPAAAAAALPMAIAAAHGNOKJAANOPPGKAAPPPPGKAAPPPPGKAAPPPPGKAAPPPPGKAAPPOPFKAAOPKFKDAAKFAAAAAAAAAAAAAAKBAAAAAANJAAAAAABPAAAAAABPAAAAAAAMJBPAAALELMEIAALMPPGKAAPPPPGKAAPPPPGKAAPPPPGKAAPPPPGKAAPPPPGKAAPPPPGKAAPPINMIAAINAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");

icon.Add(width, data);

customIcon.Add(icon);
newElement.Add(customIcon);

newElement.Add(classAt, idAt, paramAt, custIdAt);

                    xDoc.Element("LastLayout2").Element("CommandBars").Elements("CommandBar")
                        .FirstOrDefault(item => item.Attribute("Title") != null && item.Attribute("Title").Value == "Standard")
                        .Element("Controls").Add(newElement);

xDoc.Save(cust_file);

听起来像是编码问题。您可能将其保存为与原始文档不同的编码,并且与目标应用程序所期望的不同。因此文件大小发生变化。

可以按照上面 dbc 的描述更改用于保存的编码:

Create an XmlWriterSettings, set XmlWriterSettings.Encoding as appropriate, then create an XmlWriter and pass it to XDocument.Save().