Linq to XML正在减少文件大小

时间:2015-02-20 20:46:51

标签: c# xml linq linq-to-xml

我有一个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", "ABAAAAAAFCAAAAAAEDAAICAAAAAAABAAAAAAABAAAAAABAAAACAADAAAAAAAAAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAPPAAAAPPAAAAPPAAAAAAAAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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HAAILKGFEAAKGAAAAAAAAAAAAAAAAAAAAAANJAAAAAAPPAAAAAANMAAAAAABCAAAAAANBAAAAAABIAAAAAAPPAAAAAAHOAEKCAAMEIHOEAAIHIHOEAAIHIHOEAAIHIHOEAAIHJGDEAAJGGCJBAAGCAAAAAAAAAAAAAAFBAAAAAADOAAAAAAPPAAAAAAPPAAAAAAPPAAAAAAPPAAAAAALPMAIAAAHGNOKJAANOPPGKAAPPPPGKAAPPPPGKAAPPPPGKAAPPPPGKAAPPOPFKAAOPKFKDAAKFAAAAAAAAAAAAAAKBAAAAAANJAAAAAABPAAAAAABPAAAAAAAMJBPAAALELMEIAALMPPGKAAPPPPGKAAPPPPGKAAPPPPGKAAPPPPGKAAPPPPGKAAPPPPGKAAPPINMIAAINAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");

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);

1 个答案:

答案 0 :(得分:3)

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

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

  

创建一个XmlWriterSettings,将XmlWriterSettings.Encoding设置为   适当的,然后创建一个XmlWriter并将其传递给XDocument.Save()。