Linq to XML 正在返回 Null

Linq to XML Is Returning Null

<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 找到标题为 "Standard" 的 CommandBar,并在那里插入一个新的控制元素。但是,在我的代码中,当我尝试使用 "Where" 时,我得到了一个空值。我不确定我做错了什么。

更新

我在尝试获取属性值时得到 NULL。我发现并非所有 CommandBar 标签都具有 "Title" 作为属性。我如何忽略那些 CommandBar 标签?

您缺少 <Controls> 元素;

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

这对我有用(修复缺少的结束标记 /

已更新以处理属性为 null 的可能性。

要避免没有 Title 属性的 CommandBar 元素,只需向 Where

添加空检查
.Where(item => item.Attribute("Title") != null 
               && item.Attribute("Title").Value == "Standard")

您也可以将 .Where(lamda).FirstOrDefault() 替换为 .FirstOrDefault(lambda)

I found that not all the CommandBar tags have the "Title" as an attribute. How would I ignore those CommandBar tags?

只需将其添加到您的 Where 子句中:

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

请注意,在调用 .AddAfterSelf(newElement);

之前,您还需要进行空值检查