在 Silverlight 中自动将 XML 反序列化为类集合?

发布于 2024-07-14 21:13:54 字数 1276 浏览 3 评论 0原文

抱歉,如果这本质上是通用的,但我有一个问题,可能与我对 .NET 和 Silverlight 的一些核心底层规则缺乏了解有关。

我目前有一个基本项目,很简单:

  1. 一个 ASP.NET 通用处理程序写出 XML

  2. 在 Silverlight 中,我使用 WebClient 对象来获取XML 输出,事实上我 已经浏览过 Scott Gu 的教程: Scott Gu 的优秀教程

我使用了 LINQ TO XML 的示例,本质上是将 XML 加载到类集合中。例如:

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;

类如下(只是一个基本示例):

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

所以我的主要问题是......

有没有一种方法可以“自动生成”上面示例的类构建部分。 换句话说,如果我需要向源 XML 添加另外 10 列,我显然需要进入我的 silverlight 应用程序,更改上面的代码以扩展类以合并新字段,当然还有要映射的 LINQ to XML 查询类的新 XML 字段。

再说一次,我确信我在这里遗漏了一些基本的东西!

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!

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

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

发布评论

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

评论(1

星星的轨迹 2024-07-21 21:13:54

查看文章 http://mironabramson.com/blog/post/2008/06/Create-you-own-new-Type-and-use-it-on-run-time-( C).aspx

它解释了如何在运行时创建类型和实例。

我想动态类型会让 C# 4.0 中的事情变得更加容易。

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.

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