Manipulating existing XDocument (fails)

Posted by Daemonfire3002nd on Stack Overflow See other posts from Stack Overflow or by Daemonfire3002nd
Published on 2010-05-18T10:43:15Z Indexed on 2010/05/18 13:30 UTC
Read the original article Hit count: 155

Filed under:
|
|
|

Hi there,

I got the following Code snippet from my Silverlight Application:

var messages = from message in XcurrentMsg.Descendants("message")
                               where    DateTime.Parse(message.Attribute("timestamp").Value).CompareTo(DateTime.Parse(MessageCache.Last_Cached())) > 0
                               select new
                               {
                                   ip = message.Attribute("ip").Value,
                                   timestamp = message.Attribute("timestamp").Value,
                                   text = message.Value,
                               };
                if (messages == null)
                    throw new SystemException("No new messages recorded. Application tried to access non existing resources!");

            foreach (var message in messages)
            {
                XElement temporaryElement = new XElement("message", message.text.ToString(), new XAttribute("ip", message.ip.ToString()), new XAttribute("timestamp", message.timestamp.ToString()));
                XcurrentMsg.Element("root").Element("messages").Add(temporaryElement);

                AddMessage(BuildMessage(message.ip, message.timestamp, message.text));
                msgCount++;
            }

            MessageCache.CacheXML(XcurrentMsg);
            MessageCache.Refresh();

XcurrentMsg is a XDocument fetched from my server containing messages: Structure

<root>
    <messages>
          <message ip="" timestamp=""> Text </message>
    </messages>
</root>

I want to get all "message" newer than the last time I cached the XcurrentMsg. This works fine as long as I cut out the "XElement temporaryElement" and the "XcurrentMsg.Element...." and simply use the currentMsg string as output. But I want to have "new messages" being saved in my XcurrentMsg / Cache. Now if I do not cut this part out, my Application gets awesome crazy. I think it writes infinite elements to the XcurrentMsg without stopping.

I can not figure out what's the problem.

regards,

© Stack Overflow or respective owner

Related posts about c#

Related posts about Silverlight