gpt4 book ai didi

c# - Linq to XML 返回 Null

转载 作者:太空宇宙 更新时间:2023-11-03 21:23:56 26 4
gpt4 key购买 nike

<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”时,我得到了一个空值。我不确定我做错了什么。

更新

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

最佳答案

你错过了 <Controls>元素;

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

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

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

关于c# - Linq to XML 返回 Null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28630943/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com