c# - Linq to XML 返回 Null

标签 c# xml linq linq-to-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>

代码:

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/

相关文章:

c# - 使用 Open XML SDK 在 FieldChar 中设置结果

c# - 如何反序列化 XML 数组项?

c# - 如何将异步方法作为操作传递?

xml - 为什么我应该使用 XmlType 而不是关系数据库?

java - 如何只签署 XML 的特定部分

c# - ToList() 方法在哪里? (可查询)

c# - 如何编写动态 Lambda 表达式来访问第 N 个父实体?

c# - 对于 .NET 3.5,您将如何进行线程安全的获取或添加缓存?

c# - 如何使用 WebClient DownloadStringAsync 来避免卡住 UI?

c# - 如何从 C# 中的相对 URL 字符串获取参数?