在 Silverlight 中使用 XElement 读取 XML

发布于 2024-09-02 14:24:07 字数 70 浏览 2 评论 0原文

谁能指导我如何在 Silverlight (C#) 中使用 XElement 来读取 XML 文件。

谢谢你!

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

Thank You!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谁人与我共长歌 2024-09-09 14:24:07

这是一些示例代码:

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

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;
}
土豪我们做朋友吧 2024-09-09 14:24:07

您可以使用静态 XElement.Load 方法加载 XML,例如从文件流或直接从打包到 .XAP 的 XML 文件加载。

这是一个例子:
链接文本

XElement 上的 MSDN 页面也可能会有所帮助( Google:silverlight XElement 类)。

干杯,亚历克斯

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文