views:

1019

answers:

1

Sorry if this is generic in nature, but I have a question that maybe is related to my lack of understanding of some core underlying rules of .NET and Silverlight.

I have a basic project at the moment that simply:

  1. An ASP.NET generic handler writes out XML

  2. Within Silverlight, I am using the WebClient object to get the XML output, in fact I have been running through Scott Gu's tutorial: Scott Gu's excellent tutorial

I have used the example of LINQ TO XML to essentially load the XML into a class collection.. for example:

XDocument saleslogdata = XDocument.Parse(e.Result);

var logrecords = from data in saleslogdata.Descendants("data")
                 where data.Element("logID") != null
                 select new SalesLog
                 {
                     logID = (int)data.Element("logID"),
                     name = (string)data.Element("name"),
                 };

grdSalesLog.ItemsSource = logrecords;

The class is as follows (just a basic example):

public class SalesLog
{
    public int logID { get; set; }
    public string name { get; set; }
}

SO MY MAIN QUESTION IS THIS...

Is there a way I can "auto generate" the class building part of the above example. In otherwords say if I need to add another 10 columns to the source XML, I obviously need to go into my silverlight app, change the above code to expand the class to incorporate the new fields, and of course the LINQ to XML query to map the new XML fields to the class.

Again, I am sure I am missing something fundemental here!

+1  A: 

Check out the article http://mironabramson.com/blog/post/2008/06/Create-you-own-new-Type-and-use-it-on-run-time-(C).aspx.

It explains how to create a type and an instance at runtime.

And I guess dynamic types will make it more easier in C# 4.0.

gk
Hi thanks for your reply.. mmm I haven't read much about C# 4.0 yet, although I did hear about dynamic types and wondered what they were!The only problem is the link you provided seems dead or possibly pasted incorrectly? sounds a good read!Thanks for your reply!