开发者

Reading XML using XElement in Silverlight

Can anyo开发者_开发百科ne please guide me on how to use XElement in Silverlight (C#) to read an XML file.

Thank You!


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;
}


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜