Reading XML Using XElement in Silverlight

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

Thank!

+3
source share
2 answers

Here is a sample 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;
}
+3
source

you can use the static XElement.Load method to load the XML, for example. from a file stream or directly from an XML file packaged in .XAP.

Here is an example: link text

An MSDN page on XElement can also be useful (Google: Silverlight XElement class).

Greetings, Alex

+1
source

Source: https://habr.com/ru/post/1745927/


All Articles