我有一个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']");
Console.WriteLine(refXElement);
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", "ABAAAAAAFCAAAAAAEDAAICAAAAAAABAAAAAAABAAAAAABAAAACAADAAAAAAAAAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAPPAAAAPPAAAAPPAAAAAAAAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAPGAAAAAAJNAAAAAAFCAAAAAAAAAAAAA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CJBAAGCAAAAAAAAAAAAAAFBAAAAAADOAAAAAAPPAAAAAAPPAAAAAAPPAAAAAAPPAAAAAALPMAIAAAHGNOKJAANOPPGKAAPPPPGKAAPPPPGKAAPPPPGKAAPPPPGKAAPPOPFKAAOPKFKDAAKFAAAAAAAAAAAAAAKBAAAAAANJAAAAAABPAAAAAABPAAAAAAAMJBPAAALELMEIAALMPPGKAAPPPPGKAAPPPPGKAAPPPPGKAAPPPPGKAAPPPPGKAAPPPPGKAAPPINMIAAINAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
icon.Add(width, data);
customIcon.Add(icon);
newElement.Add(customIcon);
newElement.Add(classAt, idAt, paramAt, custIdAt);
refXElement.Add(newElement);
xDoc.Save(cust_file);
这会将元素添加到XML文件中。但是,通过代码添加时,正在读取xml文件的应用程序似乎看不到该元素。但是,如果我手动复制并粘贴元素,应用程序确实会看到新元素。我不确定我的代码是否出错了。
原始XML
<LastLayout2 CompactMode="1" Schema="37">
<CommandBars>
<CommandBar Class="CXTPToolBar" BarID="200" Flags="63" Style="4194304" Title="Standard" MRUWidth="32767">
<Controls>
<Control Class="CXTPControlButton" Id="5864" Parameter="3000" />
<Control Class="CXTPControlButton" Id="5866" Parameter="3001" />
<Control Class="CXTPControlButton" Id="5868" Parameter="3002" />
<Control Class="CXTPControlButton" Id="24322" BeginGroup="1" Parameter="3003" />
<Control Class="CXTPControlButton" Id="24323" Parameter="3004" />
<Control Class="CXTPControlButton" Id="24324" Parameter="3005" />
<Control Class="CXTPControlButton" Id="24325" Parameter="3006" />
<Control Class="CXTPControlButton" Id="24321" BeginGroup="1" TooltipText="Undo" DescriptionText="Undo" Parameter="3007" />
<Control Class="CXTPControlButton" Id="24329" TooltipText="Redo" DescriptionText="Redo" Parameter="3008" />
<Control Class="CXTPControlButton" Id="6184" BeginGroup="1" Parameter="3009" />
<Control Class="CXTPControlButton" Id="6190" Parameter="3010" />
<Control Class="CXTPControlButton" Id="6148" Parameter="3011" />
<Control Class="CXTPControlButton" Id="6211" BeginGroup="1" Parameter="3012" />
<Control Class="CXTPControlButton" Id="5796" BeginGroup="1" Parameter="3013" />
<Control Class="CXTPControlButton" Id="5774" Parameter="3014" />
<Control Class="CXTPControlButton" Id="5792" Parameter="3015" />
<Control Class="CXTPControlButton" Id="5048" Parameter="3016" />
<Control Class="CXTPControlButton" Id="5130" BeginGroup="1" Parameter="3017" />
<Control Class="CXTPControlButton" Id="5936" Parameter="3018" />
<Control Class="CXTPControlButton" Id="5844" Parameter="3019" />
<Control Class="CXTPControlButton" Id="6170" BeginGroup="1" Parameter="3020" />
<Control Class="CXTPControlButton" Id="6182" Parameter="3021" />
<Control Class="CXTPControlButton" Id="6384" BeginGroup="1" Parameter="3022" />
<Control Class="CXTPControlButton" Id="6385" Parameter="3023" />
<Control Class="CXTPControlButton" Id="6386" Parameter="3024" />
<Control Class="CXTPControlButton" Id="32769" BeginGroup="1" Parameter="3025" />
</Controls>
</CommandBar>
</CommandBars>
</LastLayout2>
所需的XML:
<LastLayout2 CompactMode="1" Schema="37">
<CommandBars>
<CommandBar Class="CXTPToolBar" BarID="200" Flags="63" Style="4194304" Title="Standard" MRUWidth="32767">
<Controls>
<Control Class="CXTPControlButton" Id="5864" Parameter="3000" />
<Control Class="CXTPControlButton" Id="5866" Parameter="3001" />
<Control Class="CXTPControlButton" Id="5868" Parameter="3002" />
<Control Class="CXTPControlButton" Id="24322" BeginGroup="1" Parameter="3003" />
<Control Class="CXTPControlButton" Id="24323" Parameter="3004" />
<Control Class="CXTPControlButton" Id="24324" Parameter="3005" />
<Control Class="CXTPControlButton" Id="24325" Parameter="3006" />
<Control Class="CXTPControlButton" Id="24321" BeginGroup="1" TooltipText="Undo" DescriptionText="Undo" Parameter="3007" />
<Control Class="CXTPControlButton" Id="24329" TooltipText="Redo" DescriptionText="Redo" Parameter="3008" />
<Control Class="CXTPControlButton" Id="6184" BeginGroup="1" Parameter="3009" />
<Control Class="CXTPControlButton" Id="6190" Parameter="3010" />
<Control Class="CXTPControlButton" Id="6148" Parameter="3011" />
<Control Class="CXTPControlButton" Id="6211" BeginGroup="1" Parameter="3012" />
<Control Class="CXTPControlButton" Id="5796" BeginGroup="1" Parameter="3013" />
<Control Class="CXTPControlButton" Id="5774" Parameter="3014" />
<Control Class="CXTPControlButton" Id="5792" Parameter="3015" />
<Control Class="CXTPControlButton" Id="5048" Parameter="3016" />
<Control Class="CXTPControlButton" Id="5130" BeginGroup="1" Parameter="3017" />
<Control Class="CXTPControlButton" Id="5936" Parameter="3018" />
<Control Class="CXTPControlButton" Id="5844" Parameter="3019" />
<Control Class="CXTPControlButton" Id="6170" BeginGroup="1" Parameter="3020" />
<Control Class="CXTPControlButton" Id="6182" Parameter="3021" />
<Control Class="CXTPControlButton" Id="6384" BeginGroup="1" Parameter="3022" />
<Control Class="CXTPControlButton" Id="6385" Parameter="3023" />
<Control Class="CXTPControlButton" Id="6386" Parameter="3024" />
<Control Class="CXTPControlButton" Id="32769" BeginGroup="1" Parameter="3025" />
<Control Class="CXTPControlButton" Id="0" Parameter="GLOBAL!QMS_Launcher.Main" CustomIconId="68267">
<CustomIcon>
<Icon Width="16" Data="ABAAAAAAFCAAAAAAEDAAICAAAAAAABAAAAAAABAAAAAABAAAACAADAAAAAAAAAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAPPAAAAPPAAAAPPAAAAAAAAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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