Search code examples
c#xmlxelementxattribute

How to properly access/modify an attribute in XElement object


I have some code that reads in a nicely formatted XML file. It wraps each XElement it reads into my object (ScriptEventNode) where I have some fields to allow the caller to change some of the Element and Attribute values that matter to us.

I also have the ability to create a node of my own with the same root element name, "Event" to create comments, Region_start and Region_end.

Most of the code seems to work, but one is giving me issues. One of the fields I'm setting/changing is a timestamp. When I come across one of my ScriptEventNodes that came from the original file, it sets the timestamp just fine, but when I get to one of those that I created (like a comment node), it gives a null reference exception.

The code that sets the timestamp is here:

// Timestamp range must be from 1/1/01 00:00:00 to 1/1/01 23:59:59
private DateTime _timestamp;
public DateTime Timestamp
{
    set
    {
        DateTime minDT = DateTime.ParseExact(InitialTimeStamp, "yyyy-MM-ddTHH:mm:ss", CultureInfo.InvariantCulture);
        DateTime maxDT = DateTime.ParseExact("2000-01-01T23:59:59", "yyyy-MM-ddTHH:mm:ss", CultureInfo.InvariantCulture);
        if (value >= minDT && value <= maxDT) // validate within timestamp range
        {
            try
            {
                string ts = value.ToString("yyyy-MM-ddTHH:mm:ss", CultureInfo.InvariantCulture);
                _myXElement.Attribute("TimeStamp").Value = ts;
                _timestamp = value;
            }
            catch (Exception e)
            {
                LogManager.LogExceptionMessage(e);
            }                                        
        }
    }
    get
    {
        try
        {
            string ts = _myXElement.Attribute("TimeStamp").Value;
            return DateTime.ParseExact(ts, "yyyy-MM-ddTHH:mm:ss", CultureInfo.InvariantCulture);
        }
        catch (Exception e)
        {
            LogManager.LogExceptionMessage(e);                    
        }
        return DateTime.ParseExact(InitialTimeStamp, "yyyy-MM-ddTHH:mm:ss", CultureInfo.InvariantCulture);
    }

}

Coming to this code with a regular event the xml in _myXElement looks like this (as one example):

<Event TimeStamp="2017-08-03T09:19:28" TimeFraction="545302" Index="0">
  <EventOriginatorInfo SoftwareModule="PercuNav" />
  <EventInfo InfoCategory="UIEvent" LogCategory="Workflow" EventCategory="Information" EventID="600000.J329.LXBW.0" Description="Successfully loaded bCore.dll" >
    <AdditionalInfo  >
      <MESSAGE_CATEGORY>PNAP</MESSAGE_CATEGORY>
    </AdditionalInfo>
  </EventInfo>
</Event>

One of the ones that I created (like a comment node, for example) looks like this:

<Event Timestamp="2000-01-01T00:00:00" TimeFraction="000000" Index="0">
    <COMMENT>Spiffy little UISCRIPT comment!!</COMMENT>
  </Event>

For the life of me, I'm not seeing why I get the exception on the line:

_myXElement.Attribute("TimeStamp").Value = ts;

Hoping somebody can see what the difference may be.

I've tried a debug line right before it to do the same thing, just to look at the XAttribute.

XAttribute a = _myXElement.Attribute("TimeStamp");

As you can guess, the value of a is null for one, but not the other.

I would think these should work exactly the same way, as it's an attribute off the same type of XElement, specifically "Event".


Solution

  • Since I can't mark it as answer for some reason, I'll have to use the "answer your own question" option.

    Beibeizhu was correct. It was simply a case sensitivity problem, "TimeStamp" vs. "Timestamp".