Search code examples
c#xelement

Conditionally adding a XElement to a new XElement using c~


I am trying to use XElement to add new content to an existing XML data file.

For the most part it is working OK but I need to change it. This is what I have so far:

xdoc.Root.Add(
    new XElement("W" + record.Date.ToString("yyyyMMdd"),
        new XElement("Chairman", historyItem.Chairman),
        new XElement("AuxCounsellor1", historyItem.AuxCounsellor1),
        new XElement("AuxCounsellor2", historyItem.AuxCounsellor2),
        new XElement("VideoConferenceHost", string.Empty),
        new XElement("VideoConferenceCohost", string.Empty),
        new XElement("PrayerOpen", historyItem.PrayerOpen),
        new XElement("PrayerClose", historyItem.PrayerClose),
        new XElement("CBSConductor", historyItem.CBSConductor),
        new XElement("CBSReader", historyItem.CBSReader),
        new XElement("ReviewQuestion", string.Empty),
        new XElement("Items",
            new XAttribute("ItemCount", historyItem.TalkItems.Count),
            new XElement("Item",
                new XElement("Name", historyItem.TalkItems[0].Name),
                new XElement("Theme", historyItem.TalkItems[0].Theme),
                new XElement("Method", historyItem.TalkItems[0].Method)),
            new XElement("Item",
                new XElement("Name", historyItem.TalkItems[1].Name),
                new XElement("Theme", historyItem.TalkItems[1].Theme),
                new XElement("Method", historyItem.TalkItems[1].Method)),
            new XElement("Item",
                new XElement("Name", historyItem.TalkItems[2].Name),
                new XElement("Theme", historyItem.TalkItems[2].Theme),
                new XElement("Method", historyItem.TalkItems[2].Method)),
            new XElement("Item",
                new XElement("Name", historyItem.TalkItems[3].Name),
                new XElement("Theme", historyItem.TalkItems[3].Theme),
                new XElement("Method", historyItem.TalkItems[3].Method)),
            new XElement("Item",
                new XElement("Name", historyItem.TalkItems[4].Name),
                new XElement("Theme", historyItem.TalkItems[4].Theme),
                new XElement("Method", historyItem.TalkItems[4].Method)),
            new XElement("Item",
                new XElement("Name", historyItem.TalkItems[5].Name),
                new XElement("Theme", historyItem.TalkItems[5].Theme),
                 new XElement("Method", historyItem.TalkItems[5].Method)))));

It creates an entry like this:

  <W20220404>
    <Chairman></Chairman>
    <AuxCounsellor1></AuxCounsellor1>
    <AuxCounsellor2></AuxCounsellor2>
    <VideoConferenceHost></VideoConferenceHost>
    <VideoConferenceCohost></VideoConferenceCohost>
    <PrayerOpen></PrayerOpen>
    <PrayerClose></PrayerClose>
    <CBSConductor></CBSConductor>
    <CBSReader></CBSReader>
    <ReviewQuestion></ReviewQuestion>
    <Items ItemCount="6">
      <Item>
        <Name></Name>
        <Theme>“Come essere un vero amico”</Theme>
        <Method>Talk</Method>
      </Item>
      <Item>
        <Name></Name>
        <Theme>Spiritual Gems</Theme>
        <Method>Question and Answers</Method>
      </Item>
      <Item>
        <Name></Name>
        <Theme>Prepare This Month's Presentations</Theme>
        <Method>Discussion with Video</Method>
      </Item>
      <Item>
        <Name></Name>
        <Theme>“Chi sono i tuoi amici online?”</Theme>
        <Method>Discussion with Video</Method>
      </Item>
      <Item>
        <Name></Name>
        <Theme>Diamo il benvenuto agli ospiti</Theme>
        <Method>Discussion with Video</Method>
      </Item>
      <Item>
        <Name></Name>
        <Theme></Theme>
        <Method>Talk</Method>
      </Item>
    </Items>
  </W20220404>

Now, I want to add another item into this W20220404 node, just after the list of Items. Eg:

<Teaching>
    <Name Class="0">Brother 9</Name>
    <Name Class="1">Brother 1</Name>
    <Name Class="2">Brother 3</Name>
</Teaching>

But:

  1. I only want to add the first Name if historyItem.Classes >= 1.
  2. I only want to add the second Name if historyItem.Classes >= 2.
  3. I only want to add the third Name if historyItem.Classes == 3.

I can't work out how to attach that. Initially I tried extending my code:

new XElement("Teaching",
    new XElement("Name",
        new XAttribute("Class", 0),
        new XText(historyItem.Teaching[0])),
    new XElement("Name",
        new XAttribute("Class", 1),
        new XText(historyItem.Teaching[1])),
    new XElement("Name",
        new XAttribute("Class", 2),
        new XText(historyItem.Teaching[2])))));

But it doesn't factor in for the logical requirements. What is the sensible way to add this node using the rules indicated?

To summarize:

Create the Teaching node
if Teaching is not null
    if historyItem.Classes >= 1
         Add the first Name / attribute
    if historyItem.Classes >= 2
         Add the second Name / attribute
    if historyItem.Classes == 3
         Add the third Name / attribute

So if the Teaching element is null I want an empty Teaching element in XML.

Making sense?


Solution

  • You can take advantage of the fact that null nodes are ignored, so make a condition where you pass null when the condition is not met. Here is the basic idea:

    new XElement("Teaching",
        historyItem.Classes >= 1 ? new XElement("Name",
            new XAttribute("Class", 0),
            new XText(historyItem.Teaching[0])) : null,
    

    After reading the comment, it appears that you don't even need a conditional and could do the following:

    historyItem.Teaching != null ? new XElement("Teaching",
        new XElement("Name",
            new XAttribute("Class", historyItem.Classes - 1),
            new XText(historyItem.Teaching[historyItem.Classes - 1]))
            ), null));