LINQ to XML in Silverlight – Not! (Well, not yet…)

Hey, so I’m not a walking encyclopedia.

I thought it would be cool to try to build an xml file from a Silverlight object – namely the StrokeColleciton of an InkPresenter.

I coded it up in what I thought was a very nice way:

   XElement XMLStrokes = new XElement(“StrokeCollection”);
   //create stroke then add to collection element   
   XElement mystroke;
   foreach (Stroke s in mystrokes)
   {
    mystroke = new XElement(“Stroke”,
     new XElement(“Stroke.DrawingAttributes”,
      new XElement(“DrawingAttributes”,
        new XAttribute(“Color”, s.DrawingAttributes.Color),
        new XAttribute(“OutlineColor”, s.DrawingAttributes.OutlineColor),
        new XAttribute(“Width”, s.DrawingAttributes.Width),
        new XAttribute(“Height”, s.DrawingAttributes.Height))));

    //create points separatly then add to mystroke xelement
    XElement myPoints = new XElement(“Stroke.StylusPoints”);
    foreach (StylusPoint sp in s.StylusPoints)
    {
     XElement mypoint = new XElement(“Stylus Point”,
      new XAttribute(“x”, sp.X),
      new XAttribute(“y”, sp.Y));
     myPoints.Add(mypoint);
    }
    mystroke.Add(myPoints);
    XMLStrokes.Add(mystroke);
   }

It compiled fine, but then the strangest thing happened. The code would never get called. If I put it in a method and had a click event call the method, the method would not get hit. I put it in a new static class but still it did not get hit. Then I put the code right in the click event and then the click event stopped getting hit.

I thought for sure it was me being stupid with C#. But finally I had a new idea and I googled “LINQ to XML” and Silverlight and the first hit was a post by Fabrice Marguerie saying that LINQ to XML was not supported in Silverlight, with a link to a post by Aaron Dunnington on the XML team.

Not supported, eh? Well, it’s just not quite yet. it’ll be there. (I knew that from reading Aaron’s post, but totally neglected to point that out. Thanks for Scott Guthrie’s reminder in the comments.) Either way, it sure had a funny way of showing it. Sheesh!

I suppose I could put it into a WPF project and test it (I still don’t know if my logic is correct) but I’ve got better things to do at the moment.

Note: I did think of using a webservice for this, but a StrokeCollecton is not serializable, so it would be redundant to find another way to get it across the pipe just to see if I can pull it off in LINQ to XML while still in the context of a Silverlight app. I’ll just be patient.

  Sign up for my newsletter so you don't miss my conference & Pluralsight course announcements!  

One thought on “LINQ to XML in Silverlight – Not! (Well, not yet…)

  1. LINQ to XML isn’t currently supported in the Silverlight 1.1 Alpha – but will be supported in the final Silverlight 1.1 release.Hope this helps,Scott

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.