user2970916
user2970916

Reputation: 1186

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>

Code:

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

I am trying to use Linq to XML to find the CommandBar with Title="Standard", and insert a new control element there. However, in my code, when I attempt to use "Where", I am getting a null value. I am not sure what I am doing wrong.

UPDATE

I am getting a NULL when trying to get the Attributes value. I found that not all the CommandBar tags have the "Title" as an attribute. How would I ignore those CommandBar tags?

Upvotes: 1

Views: 148

Answers (3)

juharr
juharr

Reputation: 32266

To avoid CommandBar elements that do not have a Title attribute just add a null check to your Where

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

Also you can replace the .Where(lamda).FirstOrDefault() with just .FirstOrDefault(lambda).

Upvotes: 1

Alex K.
Alex K.

Reputation: 175766

Your missing the <Controls> element;

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

This works for me (fixing the closing tag which is missing /)

Updated to deal with the possibility of the attrib being null.

Upvotes: 2

D Stanley
D Stanley

Reputation: 152521

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

Just add that to your Where clause:

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

Note that you'll also need a null check before calling .AddAfterSelf(newElement);

Upvotes: 1

Related Questions