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


码:

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", "AAB");

icon.Add(width, data);

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

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

xDoc.Element("LastLayout2")
    .Element("CommandBars")
    .Elements("CommandBar")
    .Where(item => item.Attribute("Title").Value == "Standard").FirstOrDefault()
    .AddAfterSelf(newElement);

xDoc.Save(cust_file);


我正在尝试使用Linq to XML查找带有Title =“ Standard”的CommandBar,并在其中插入新的控件元素。但是,在我的代码中,当我尝试使用“ Where”时,我得到的是空值。我不确定自己在做什么错。

更新

尝试获取Attributes值时出现NULL。我发现并非所有CommandBar标记都具有“标题”作为属性。我将如何忽略那些CommandBar标记?

最佳答案

为避免不具有CommandBar属性的Title元素,只需在Where中添加一个空检查

.Where(item => item.Attribute("Title") != null
               && item.Attribute("Title").Value == "Standard")


您也可以仅用.Where(lamda).FirstOrDefault()替换.FirstOrDefault(lambda)

07-26 00:05