Jayesh
Jayesh

Reputation: 3951

Reading XML using XElement in Silverlight

Can anyone please guide me on how to use XElement in Silverlight (C#) to read an XML file.

Thank You!

Upvotes: 3

Views: 3313

Answers (2)

alexander.biskop
alexander.biskop

Reputation: 1862

you can use the static XElement.Load method to load XML e.g. from a file stream or directly from an XML file packaged into the .XAP.

Here's an example: link text

The MSDN page on XElement might also be helpful (Google: silverlight XElement class).

Cheers, Alex

Upvotes: 1

Peter McG
Peter McG

Reputation: 19045

Here's some example code:

private void Page_Loaded(object sender, RoutedEventArgs e)
{
    DataGrid1.ItemsSource = GetStatusReport();
}

public List<Status> GetStatusReport()
{
    List<Status> statusReport = new List<Status>();

    XElement doc = XElement.Load(@"Data/StatusReport.xml");

    statusReport = (from el in doc.Elements()
                    select GetStatus(el)).ToList();

    return statusReport;
}

private Status GetStatus(XElement el)
{
    Status s = new Status();
    s.Description = el.Attribute("Description").Value;
    s.Date = DateTime.Parse(el.Attribute("Date").Value);
    return s;
}

Upvotes: 3

Related Questions